001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.http.server.repository;
007:
008: import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
009:
010: import javax.servlet.http.HttpServletRequest;
011: import javax.servlet.http.HttpServletResponse;
012:
013: import org.slf4j.Logger;
014: import org.slf4j.LoggerFactory;
015:
016: import org.openrdf.http.protocol.Protocol;
017: import org.openrdf.http.server.ClientHTTPException;
018: import org.openrdf.http.server.ServerHTTPException;
019: import org.openrdf.http.server.ProtocolUtil;
020: import org.openrdf.http.server.ServerInterceptor;
021: import org.openrdf.repository.Repository;
022: import org.openrdf.repository.RepositoryConnection;
023: import org.openrdf.repository.RepositoryException;
024: import org.openrdf.repository.config.RepositoryConfigException;
025: import org.openrdf.repository.manager.LocalRepositoryManager;
026:
027: /**
028: * Interceptor for repository requests. Handles the opening and closing of
029: * connections to the repository specified in the request. Should not be a
030: * singleton bean! Configure as inner bean in openrdf-servlet.xml
031: *
032: * @author Herko ter Horst
033: * @author Arjohn Kampman
034: */
035: public class RepositoryInterceptor extends ServerInterceptor {
036:
037: /*-----------*
038: * Constants *
039: *-----------*/
040:
041: private final Logger logger = LoggerFactory.getLogger(this
042: .getClass());
043:
044: private static final String REPOSITORY_ID_KEY = "repositoryID";
045:
046: private static final String REPOSITORY_KEY = "repository";
047:
048: private static final String REPOSITORY_CONNECTION_KEY = "repositoryConnection";
049:
050: /*-----------*
051: * Variables *
052: *-----------*/
053:
054: private LocalRepositoryManager repositoryManager;
055:
056: private String repositoryID;
057:
058: private RepositoryConnection repositoryCon;
059:
060: /*---------*
061: * Methods *
062: *---------*/
063:
064: public void setRepositoryManager(LocalRepositoryManager repMan) {
065: repositoryManager = repMan;
066: }
067:
068: @Override
069: public boolean preHandle(HttpServletRequest request,
070: HttpServletResponse respons, Object handler)
071: throws Exception {
072: String pathInfoStr = request.getPathInfo();
073: logger.debug("path info: {}", pathInfoStr);
074:
075: repositoryID = null;
076:
077: if (pathInfoStr != null && !pathInfoStr.equals("/")) {
078: String[] pathInfo = pathInfoStr.substring(1).split("/");
079: if (pathInfo.length > 0) {
080: repositoryID = pathInfo[0];
081: logger.debug("repositoryID is '{}'", repositoryID);
082: }
083: }
084:
085: ProtocolUtil.logRequestParameters(request);
086:
087: return super .preHandle(request, respons, handler);
088: }
089:
090: @Override
091: protected String getThreadName() {
092: String threadName = Protocol.REPOSITORIES;
093:
094: if (repositoryID != null) {
095: threadName += "/" + repositoryID;
096: }
097:
098: return threadName;
099: }
100:
101: @Override
102: protected void setRequestAttributes(HttpServletRequest request)
103: throws ClientHTTPException, ServerHTTPException {
104: if (repositoryID != null) {
105: try {
106: Repository repository = repositoryManager
107: .getRepository(repositoryID);
108:
109: if (repository == null) {
110: throw new ClientHTTPException(SC_NOT_FOUND,
111: "Unknown repository: " + repositoryID);
112: }
113:
114: repositoryCon = repository.getConnection();
115: request.setAttribute(REPOSITORY_ID_KEY, repositoryID);
116: request.setAttribute(REPOSITORY_KEY, repository);
117: request.setAttribute(REPOSITORY_CONNECTION_KEY,
118: repositoryCon);
119: } catch (RepositoryConfigException e) {
120: throw new ServerHTTPException(e.getMessage(), e);
121: } catch (RepositoryException e) {
122: throw new ServerHTTPException(e.getMessage(), e);
123: }
124: }
125: }
126:
127: @Override
128: protected void cleanUpResources() throws ServerHTTPException {
129: if (repositoryCon != null) {
130: try {
131: repositoryCon.close();
132: } catch (RepositoryException e) {
133: throw new ServerHTTPException(e.getMessage(), e);
134: }
135: }
136: }
137:
138: public static String getRepositoryID(HttpServletRequest request) {
139: return (String) request.getAttribute(REPOSITORY_ID_KEY);
140: }
141:
142: public static Repository getRepository(HttpServletRequest request) {
143: return (Repository) request.getAttribute(REPOSITORY_KEY);
144: }
145:
146: public static RepositoryConnection getRepositoryConnection(
147: HttpServletRequest request) {
148: return (RepositoryConnection) request
149: .getAttribute(REPOSITORY_CONNECTION_KEY);
150: }
151: }
|