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.namespaces;
007:
008: import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
009: import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
010:
011: import java.io.IOException;
012: import java.util.HashMap;
013: import java.util.Map;
014:
015: import javax.servlet.http.HttpServletRequest;
016: import javax.servlet.http.HttpServletResponse;
017:
018: import org.slf4j.Logger;
019: import org.slf4j.LoggerFactory;
020: import org.springframework.context.ApplicationContextException;
021: import org.springframework.web.servlet.ModelAndView;
022: import org.springframework.web.servlet.mvc.AbstractController;
023:
024: import info.aduna.io.IOUtil;
025: import info.aduna.webapp.views.EmptySuccessView;
026: import info.aduna.webapp.views.SimpleResponseView;
027:
028: import org.openrdf.http.server.ClientHTTPException;
029: import org.openrdf.http.server.ServerHTTPException;
030: import org.openrdf.http.server.repository.RepositoryInterceptor;
031: import org.openrdf.repository.RepositoryConnection;
032: import org.openrdf.repository.RepositoryException;
033:
034: /**
035: * Handles requests for manipulating a specific namespace definition in a
036: * repository.
037: *
038: * @author Herko ter Horst
039: * @author Arjohn Kampman
040: */
041: public class NamespaceController extends AbstractController {
042:
043: private Logger logger = LoggerFactory.getLogger(this .getClass());
044:
045: public NamespaceController() throws ApplicationContextException {
046: setSupportedMethods(new String[] { METHOD_GET, "PUT", "DELETE" });
047: }
048:
049: @Override
050: protected ModelAndView handleRequestInternal(
051: HttpServletRequest request, HttpServletResponse response)
052: throws Exception {
053: String pathInfoStr = request.getPathInfo();
054: String[] pathInfo = pathInfoStr.substring(1).split("/");
055: String prefix = pathInfo[pathInfo.length - 1];
056:
057: RepositoryConnection repositoryCon = RepositoryInterceptor
058: .getRepositoryConnection(request);
059:
060: String reqMethod = request.getMethod();
061: if (METHOD_GET.equals(reqMethod)) {
062: logger.info("GET namespace for prefix {}" + prefix);
063: return getExportNamespaceResult(repositoryCon, prefix);
064: } else if ("PUT".equals(reqMethod)) {
065: logger.info("PUT prefix {}", prefix);
066: return getUpdateNamespaceResult(repositoryCon, prefix,
067: request);
068: } else if ("DELETE".equals(reqMethod)) {
069: logger.info("DELETE prefix {}", prefix);
070: return getRemoveNamespaceResult(repositoryCon, prefix);
071: } else {
072: throw new ServerHTTPException("Unexpected request method: "
073: + reqMethod);
074: }
075: }
076:
077: private ModelAndView getExportNamespaceResult(
078: RepositoryConnection repositoryCon, String prefix)
079: throws ServerHTTPException, ClientHTTPException {
080: try {
081: String namespace = repositoryCon.getNamespace(prefix);
082:
083: if (namespace == null) {
084: throw new ClientHTTPException(SC_NOT_FOUND,
085: "Undefined prefix: " + prefix);
086: }
087:
088: Map<String, Object> model = new HashMap<String, Object>();
089: model.put(SimpleResponseView.CONTENT_KEY, namespace);
090:
091: return new ModelAndView(SimpleResponseView.getInstance(),
092: model);
093: } catch (RepositoryException e) {
094: throw new ServerHTTPException("Repository error: "
095: + e.getMessage(), e);
096: }
097: }
098:
099: private ModelAndView getUpdateNamespaceResult(
100: RepositoryConnection repositoryCon, String prefix,
101: HttpServletRequest request) throws IOException,
102: ClientHTTPException, ServerHTTPException {
103: String namespace = IOUtil.readString(request.getReader());
104: namespace = namespace.trim();
105:
106: if (namespace.length() == 0) {
107: throw new ClientHTTPException(SC_BAD_REQUEST,
108: "No namespace name found in request body");
109: }
110: // FIXME: perform some sanity checks on the namespace string
111:
112: try {
113: repositoryCon.setNamespace(prefix, namespace);
114: } catch (RepositoryException e) {
115: throw new ServerHTTPException("Repository error: "
116: + e.getMessage(), e);
117: }
118:
119: return new ModelAndView(EmptySuccessView.getInstance());
120: }
121:
122: private ModelAndView getRemoveNamespaceResult(
123: RepositoryConnection repositoryCon, String prefix)
124: throws ServerHTTPException {
125: try {
126: repositoryCon.removeNamespace(prefix);
127: } catch (RepositoryException e) {
128: throw new ServerHTTPException("Repository error: "
129: + e.getMessage(), e);
130: }
131:
132: return new ModelAndView(EmptySuccessView.getInstance());
133: }
134: }
|