001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 2006-2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.repository.http;
007:
008: import java.io.File;
009: import java.io.IOException;
010:
011: import org.openrdf.http.client.HTTPClient;
012: import org.openrdf.model.Value;
013: import org.openrdf.model.ValueFactory;
014: import org.openrdf.model.impl.ValueFactoryImpl;
015: import org.openrdf.model.util.LiteralUtil;
016: import org.openrdf.query.BindingSet;
017: import org.openrdf.query.QueryEvaluationException;
018: import org.openrdf.query.TupleQueryResult;
019: import org.openrdf.query.resultio.TupleQueryResultFormat;
020: import org.openrdf.repository.Repository;
021: import org.openrdf.repository.RepositoryConnection;
022: import org.openrdf.repository.RepositoryException;
023: import org.openrdf.rio.RDFFormat;
024:
025: /**
026: * A repository that serves as a proxy for a remote repository on a Sesame
027: * server.
028: *
029: * Methods in this class may throw the specific RepositoryException subclasses
030: * UnautorizedException and NotAllowedException, the semantics of which are
031: * defined by the HTTP protocol.
032: *
033: * @see org.openrdf.http.protocol.UnauthorizedException
034: * @see org.openrdf.http.protocol.NotAllowedException
035: *
036: * @author Arjohn Kampman
037: * @author jeen
038: * @author Herko ter Horst
039: */
040: public class HTTPRepository implements Repository {
041:
042: /*-----------*
043: * Variables *
044: *-----------*/
045:
046: /**
047: * The HTTP client that takes care of the client-server communication.
048: */
049: private HTTPClient httpClient;
050:
051: private File dataDir;
052:
053: private boolean initialized = false;
054:
055: /*--------------*
056: * Constructors *
057: *--------------*/
058:
059: private HTTPRepository() {
060: httpClient = new HTTPClient();
061: httpClient.setValueFactory(new ValueFactoryImpl());
062: }
063:
064: public HTTPRepository(String serverURL, String repositoryID) {
065: this ();
066: httpClient.setServerURL(serverURL);
067: httpClient.setRepositoryID(repositoryID);
068: }
069:
070: public HTTPRepository(String repositoryURL) {
071: this ();
072: httpClient.setRepositoryURL(repositoryURL);
073: }
074:
075: /*---------*
076: * Methods *
077: *---------*/
078:
079: // httpClient is shared with HTTPConnection
080: HTTPClient getHTTPClient() {
081: return httpClient;
082: }
083:
084: public void setDataDir(File dataDir) {
085: this .dataDir = dataDir;
086: }
087:
088: public File getDataDir() {
089: return dataDir;
090: }
091:
092: public void initialize() throws RepositoryException {
093: initialized = true;
094: }
095:
096: public void shutDown() throws RepositoryException {
097: initialized = false;
098: }
099:
100: public ValueFactory getValueFactory() {
101: return httpClient.getValueFactory();
102: }
103:
104: public RepositoryConnection getConnection()
105: throws RepositoryException {
106: return new HTTPRepositoryConnection(this );
107: }
108:
109: public boolean isWritable() throws RepositoryException {
110: if (!initialized) {
111: throw new IllegalStateException(
112: "HTTPRepository not initialized.");
113: }
114:
115: boolean isWritable = false;
116: String repositoryURL = httpClient.getRepositoryURL();
117:
118: try {
119: TupleQueryResult repositoryList = httpClient
120: .getRepositoryList();
121: try {
122: while (repositoryList.hasNext()) {
123: BindingSet bindingSet = repositoryList.next();
124: Value uri = bindingSet.getValue("uri");
125:
126: if (uri != null
127: && uri.stringValue().equals(repositoryURL)) {
128: isWritable = LiteralUtil.getBooleanValue(
129: bindingSet.getValue("writable"), false);
130: break;
131: }
132: }
133: } catch (QueryEvaluationException e) {
134: throw new RepositoryException(e);
135: } finally {
136: try {
137: repositoryList.close();
138: } catch (QueryEvaluationException e) {
139: throw new RepositoryException(e);
140: }
141: }
142: } catch (IOException e) {
143: throw new RepositoryException(e);
144: }
145:
146: return isWritable;
147: }
148:
149: /**
150: * Sets the preferred serialization format for tuple query results to the
151: * supplied {@link TupleQueryResultFormat}, overriding the
152: * {@link HTTPClient}'s default preference. Setting this parameter is not
153: * necessary in most cases as the {@link HTTPClient} by default indicates a
154: * preference for the most compact and efficient format available.
155: *
156: * @param format
157: * the preferred {@link TupleQueryResultFormat}. If set to 'null' no
158: * explicit preference will be stated.
159: */
160: public void setPreferredTupleQueryResultFormat(
161: TupleQueryResultFormat format) {
162: httpClient.setPreferredTupleQueryResultFormat(format);
163: }
164:
165: /**
166: * Indicates the current preferred {@link TupleQueryResultFormat}.
167: *
168: * @return The preferred format, of 'null' if no explicit preference is
169: * defined.
170: */
171: public TupleQueryResultFormat getPreferredTupleQueryResultFormat() {
172: return httpClient.getPreferredTupleQueryResultFormat();
173: }
174:
175: /**
176: * Sets the preferred serialization format for RDF to the supplied
177: * {@link RDFFormat}, overriding the {@link HTTPClient}'s default
178: * preference. Setting this parameter is not necessary in most cases as the
179: * {@link HTTPClient} by default indicates a preference for the most compact
180: * and efficient format available.
181: * <p>
182: * Use with caution: if set to a format that does not support context
183: * serialization any context info contained in the query result will be lost.
184: *
185: * @param format
186: * the preferred {@link RDFFormat}. If set to 'null' no explicit
187: * preference will be stated.
188: */
189: public void setPreferredRDFFormat(RDFFormat format) {
190: httpClient.setPreferredRDFFormat(format);
191: }
192:
193: /**
194: * Indicates the current preferred {@link RDFFormat}.
195: *
196: * @return The preferred format, of 'null' if no explicit preference is
197: * defined.
198: */
199: public RDFFormat getPreferredRDFFormat() {
200: return httpClient.getPreferredRDFFormat();
201: }
202:
203: /**
204: * Set the username and password to use for authenticating with the remote
205: * repository.
206: *
207: * @param username
208: * the username. Setting this to null will disable authentication.
209: * @param password
210: * the password. Setting this to null will disable authentication.
211: */
212: public void setUsernameAndPassword(String username, String password) {
213: httpClient.setUsernameAndPassword(username, password);
214: }
215: }
|