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.repository.manager;
007:
008: import java.io.IOException;
009: import java.net.MalformedURLException;
010: import java.net.URL;
011: import java.util.ArrayList;
012: import java.util.Collection;
013: import java.util.List;
014:
015: import org.openrdf.http.client.HTTPClient;
016: import org.openrdf.http.protocol.UnauthorizedException;
017: import org.openrdf.model.util.LiteralUtil;
018: import org.openrdf.query.BindingSet;
019: import org.openrdf.query.QueryEvaluationException;
020: import org.openrdf.query.TupleQueryResult;
021: import org.openrdf.repository.Repository;
022: import org.openrdf.repository.RepositoryException;
023: import org.openrdf.repository.config.RepositoryConfigException;
024: import org.openrdf.repository.config.RepositoryConfigUtil;
025: import org.openrdf.repository.http.HTTPRepository;
026:
027: /**
028: * A manager for {@link Repository}s that reside on a remote server. This
029: * repository manager allows one to access repositories over HTTP similar to how
030: * local repositories are accessed using the {@link LocalRepositoryManager}.
031: *
032: * @author Arjohn Kampman
033: */
034: public class RemoteRepositoryManager extends RepositoryManager {
035:
036: /*-----------*
037: * Variables *
038: *-----------*/
039:
040: /**
041: * The URL of the remote server, e.g. http://localhost:8080/openrdf-sesame/
042: */
043: private String serverURL;
044:
045: private String username;
046:
047: private String password;
048:
049: /*--------------*
050: * Constructors *
051: *--------------*/
052:
053: /**
054: * Creates a new RepositoryManager that operates on the specfified base
055: * directory.
056: *
057: * @param baseDir
058: * The base directory where data for repositories can be stored, among
059: * other things.
060: */
061: public RemoteRepositoryManager(String serverURL) {
062: super ();
063: this .serverURL = serverURL;
064: }
065:
066: /*---------*
067: * Methods *
068: *---------*/
069: /**
070: * Set the username and password for authenication with the remote server.
071: *
072: * @param username
073: * the username
074: * @param password
075: * the password
076: */
077: public void setUsernameAndPassword(String username, String password) {
078: this .username = username;
079: this .password = password;
080: }
081:
082: @Override
083: protected Repository createSystemRepository()
084: throws RepositoryException {
085: HTTPRepository systemRepository = new HTTPRepository(serverURL,
086: SystemRepository.ID);
087: systemRepository.initialize();
088: return systemRepository;
089: }
090:
091: /**
092: * Gets the URL of the remote server, e.g.
093: * "http://localhost:8080/openrdf-sesame/".
094: */
095: public String getServerURL() {
096: return serverURL;
097: }
098:
099: @Override
100: public HTTPRepository getSystemRepository() {
101: HTTPRepository result = (HTTPRepository) super
102: .getSystemRepository();
103: result.setUsernameAndPassword(username, password);
104: return result;
105: }
106:
107: /**
108: * Creates and initializes the repository with the specified ID.
109: *
110: * @param id
111: * A repository ID.
112: * @return The created repository, or <tt>null</tt> if no such repository
113: * exists.
114: * @throws RepositoryConfigException
115: * If no repository could be created due to invalid or incomplete
116: * configuration data.
117: */
118: @Override
119: protected Repository createRepository(String id)
120: throws RepositoryConfigException, RepositoryException {
121: Repository result = null;
122:
123: if (RepositoryConfigUtil.hasRepositoryConfig(
124: getSystemRepository(), id)) {
125: result = new HTTPRepository(serverURL, id);
126: result.initialize();
127: }
128:
129: return result;
130: }
131:
132: @Override
133: public RepositoryInfo getRepositoryInfo(String id)
134: throws RepositoryException {
135: for (RepositoryInfo repInfo : getAllRepositoryInfos()) {
136: if (repInfo.getId().equals(id)) {
137: return repInfo;
138: }
139: }
140:
141: return null;
142: }
143:
144: @Override
145: public Collection<RepositoryInfo> getAllRepositoryInfos(
146: boolean skipSystemRepo) throws RepositoryException {
147: List<RepositoryInfo> result = new ArrayList<RepositoryInfo>();
148:
149: try {
150: HTTPClient httpClient = new HTTPClient();
151: httpClient.setServerURL(serverURL);
152: httpClient.setUsernameAndPassword(username, password);
153:
154: TupleQueryResult responseFromServer = httpClient
155: .getRepositoryList();
156: while (responseFromServer.hasNext()) {
157: BindingSet bindingSet = responseFromServer.next();
158: RepositoryInfo repInfo = new RepositoryInfo();
159:
160: String id = LiteralUtil.getLabel(bindingSet
161: .getValue("id"), null);
162:
163: if (skipSystemRepo && id.equals(SystemRepository.ID)) {
164: continue;
165: }
166:
167: String uri = LiteralUtil.getLabel(bindingSet
168: .getValue("uri"), null);
169: String description = LiteralUtil.getLabel(bindingSet
170: .getValue("title"), null);
171: boolean readable = LiteralUtil.getBooleanValue(
172: bindingSet.getValue("readable"), false);
173: boolean writable = LiteralUtil.getBooleanValue(
174: bindingSet.getValue("writable"), false);
175:
176: try {
177: repInfo.setLocation(new URL(uri));
178: } catch (MalformedURLException e) {
179: logger
180: .warn(
181: "Server reported malformed repository URL: {}",
182: uri);
183: }
184:
185: repInfo.setId(id);
186: repInfo.setDescription(description);
187: repInfo.setReadable(readable);
188: repInfo.setWritable(writable);
189:
190: result.add(repInfo);
191: }
192: } catch (IOException ioe) {
193: logger.warn("Unable to retrieve list of repositories", ioe);
194: throw new RepositoryException(ioe);
195: } catch (QueryEvaluationException qee) {
196: logger.warn("Unable to retrieve list of repositories", qee);
197: throw new RepositoryException(qee);
198: } catch (UnauthorizedException ue) {
199: logger.warn(
200: "Not authorized to retrieve list of repositories",
201: ue);
202: throw new RepositoryException(ue);
203: } catch (RepositoryException re) {
204: logger.warn("Unable to retrieve list of repositories", re);
205: throw re;
206: }
207:
208: return result;
209: }
210:
211: @Override
212: protected void cleanUpRepository(String repositoryID)
213: throws IOException {
214: // do nothing
215: }
216: }
|