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 java.util.ArrayList;
009: import java.util.Arrays;
010: import java.util.HashMap;
011: import java.util.List;
012: import java.util.Map;
013:
014: import javax.servlet.http.HttpServletRequest;
015: import javax.servlet.http.HttpServletResponse;
016:
017: import org.slf4j.Logger;
018: import org.slf4j.LoggerFactory;
019: import org.springframework.context.ApplicationContextException;
020: import org.springframework.web.servlet.ModelAndView;
021: import org.springframework.web.servlet.mvc.AbstractController;
022:
023: import info.aduna.iteration.CloseableIteration;
024: import info.aduna.webapp.views.EmptySuccessView;
025:
026: import org.openrdf.http.server.ClientHTTPException;
027: import org.openrdf.http.server.ServerHTTPException;
028: import org.openrdf.http.server.ProtocolUtil;
029: import org.openrdf.http.server.repository.QueryResultView;
030: import org.openrdf.http.server.repository.RepositoryInterceptor;
031: import org.openrdf.http.server.repository.TupleQueryResultView;
032: import org.openrdf.model.Literal;
033: import org.openrdf.model.Namespace;
034: import org.openrdf.model.impl.LiteralImpl;
035: import org.openrdf.query.BindingSet;
036: import org.openrdf.query.impl.ListBindingSet;
037: import org.openrdf.query.impl.TupleQueryResultImpl;
038: import org.openrdf.query.resultio.TupleQueryResultWriterFactory;
039: import org.openrdf.query.resultio.TupleQueryResultWriterRegistry;
040: import org.openrdf.repository.RepositoryConnection;
041: import org.openrdf.repository.RepositoryException;
042:
043: /**
044: * Handles requests for the list of namespace definitions for a repository.
045: *
046: * @author Herko ter Horst
047: */
048: public class NamespacesController extends AbstractController {
049:
050: private Logger logger = LoggerFactory.getLogger(this .getClass());
051:
052: public NamespacesController() throws ApplicationContextException {
053: setSupportedMethods(new String[] { METHOD_GET, "DELETE" });
054: }
055:
056: @Override
057: protected ModelAndView handleRequestInternal(
058: HttpServletRequest request, HttpServletResponse response)
059: throws Exception {
060: String reqMethod = request.getMethod();
061: if (METHOD_GET.equals(reqMethod)) {
062: logger.info("GET namespace list");
063: return getExportNamespacesResult(request, response);
064: } else if ("DELETE".equals(reqMethod)) {
065: logger.info("DELETE namespaces");
066: return getClearNamespacesResult(request, response);
067: }
068:
069: throw new ClientHTTPException(
070: HttpServletResponse.SC_METHOD_NOT_ALLOWED,
071: "Method not allowed: " + reqMethod);
072: }
073:
074: private ModelAndView getExportNamespacesResult(
075: HttpServletRequest request, HttpServletResponse response)
076: throws ClientHTTPException, ServerHTTPException {
077: List<String> columnNames = Arrays.asList("prefix", "namespace");
078: List<BindingSet> namespaces = new ArrayList<BindingSet>();
079:
080: RepositoryConnection repositoryCon = RepositoryInterceptor
081: .getRepositoryConnection(request);
082: try {
083: CloseableIteration<? extends Namespace, RepositoryException> iter = repositoryCon
084: .getNamespaces();
085:
086: try {
087: while (iter.hasNext()) {
088: Namespace ns = iter.next();
089:
090: Literal prefix = new LiteralImpl(ns.getPrefix());
091: Literal namespace = new LiteralImpl(ns.getName());
092:
093: BindingSet bindingSet = new ListBindingSet(
094: columnNames, prefix, namespace);
095: namespaces.add(bindingSet);
096: }
097: } finally {
098: iter.close();
099: }
100: } catch (RepositoryException e) {
101: throw new ServerHTTPException("Repository error: "
102: + e.getMessage(), e);
103: }
104:
105: TupleQueryResultWriterFactory factory = ProtocolUtil
106: .getAcceptableService(request, response,
107: TupleQueryResultWriterRegistry.getInstance());
108:
109: Map<String, Object> model = new HashMap<String, Object>();
110: model.put(QueryResultView.QUERY_RESULT_KEY,
111: new TupleQueryResultImpl(columnNames, namespaces));
112: model.put(QueryResultView.FILENAME_HINT_KEY, "namespaces");
113: model.put(QueryResultView.FACTORY_KEY, factory);
114:
115: return new ModelAndView(TupleQueryResultView.getInstance(),
116: model);
117: }
118:
119: private ModelAndView getClearNamespacesResult(
120: HttpServletRequest request, HttpServletResponse response)
121: throws ServerHTTPException {
122: RepositoryConnection repositoryCon = RepositoryInterceptor
123: .getRepositoryConnection(request);
124: try {
125: repositoryCon.clearNamespaces();
126: } catch (RepositoryException e) {
127: throw new ServerHTTPException("Repository error: "
128: + e.getMessage(), e);
129: }
130:
131: return new ModelAndView(EmptySuccessView.getInstance());
132: }
133: }
|