001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.repository.clientimpl;
017:
018: import org.outerj.daisy.repository.commonimpl.RepositoryStrategy;
019: import org.outerj.daisy.repository.commonimpl.AuthenticatedUser;
020: import org.outerj.daisy.repository.commonimpl.namespace.NamespaceImpl;
021: import org.outerj.daisy.repository.RepositoryRuntimeException;
022: import org.outerj.daisy.repository.RepositoryException;
023: import org.outerj.daisy.repository.RepositoryEventType;
024: import org.outerj.daisy.repository.namespace.Namespace;
025: import org.outerj.daisy.repository.clientimpl.infrastructure.DaisyHttpClient;
026: import org.outerj.daisy.repository.clientimpl.infrastructure.AbstractRemoteStrategy;
027: import org.outerj.daisy.util.VersionHelper;
028: import org.apache.commons.httpclient.HttpMethod;
029: import org.apache.commons.httpclient.Header;
030: import org.apache.commons.httpclient.NameValuePair;
031: import org.apache.commons.httpclient.methods.GetMethod;
032: import org.apache.commons.httpclient.methods.PostMethod;
033: import org.apache.commons.httpclient.methods.DeleteMethod;
034: import org.outerx.daisy.x10.NamespaceDocument;
035: import org.outerx.daisy.x10.NamespacesDocument;
036:
037: import java.util.Properties;
038: import java.util.List;
039: import java.util.ArrayList;
040: import java.io.IOException;
041:
042: public class RemoteRepositoryStrategy extends AbstractRemoteStrategy
043: implements RepositoryStrategy {
044: public RemoteRepositoryStrategy(
045: RemoteRepositoryManager.Context context) {
046: super (context);
047: }
048:
049: public String getClientVersion(AuthenticatedUser user) {
050: Properties versionProps;
051: try {
052: versionProps = VersionHelper
053: .getVersionProperties(getClass().getClassLoader(),
054: "org/outerj/daisy/repository/clientimpl/versioninfo.properties");
055: } catch (IOException e) {
056: throw new RepositoryRuntimeException(
057: "Error getting version information.", e);
058: }
059: String version = VersionHelper.getVersion(versionProps);
060: if (version != null)
061: return version;
062: else
063: throw new RepositoryRuntimeException("Version unknown.");
064: }
065:
066: public String getServerVersion(AuthenticatedUser user) {
067: DaisyHttpClient httpClient = getClient(user);
068: // The server reports its version in a HTTP header with each request, the actual URL we request
069: // doesn't matter much
070: HttpMethod method = new GetMethod("/repository/userinfo");
071: try {
072: httpClient.executeMethod(method, null, true);
073: } catch (RepositoryException e) {
074: throw new RepositoryRuntimeException(
075: "Error getting version info.", e);
076: }
077: Header header = method.getResponseHeader("X-Daisy-Version");
078: if (header != null && header.getValue() != null
079: && header.getValue().length() > 0) {
080: String version = header.getValue();
081: int spacePos = version.indexOf(' ');
082: if (spacePos > 0)
083: return version.substring(0, spacePos);
084: else
085: return version;
086: } else {
087: throw new RepositoryRuntimeException(
088: "Repository server did not communicate version (missing X-Daisy-Version header).");
089: }
090: }
091:
092: public NamespaceImpl registerNamespace(String namespaceName,
093: String fingerprint, AuthenticatedUser user)
094: throws RepositoryException {
095: DaisyHttpClient httpClient = getClient(user);
096: HttpMethod method = new PostMethod("/repository/namespace");
097:
098: List<NameValuePair> parameters = new ArrayList<NameValuePair>();
099: parameters.add(new NameValuePair("name", namespaceName));
100: if (fingerprint != null)
101: parameters
102: .add(new NameValuePair("fingerprint", fingerprint));
103:
104: method.setQueryString(parameters.toArray(new NameValuePair[0]));
105:
106: NamespaceDocument namespaceDocument = (NamespaceDocument) httpClient
107: .executeMethod(method, NamespaceDocument.class, true);
108:
109: NamespaceImpl namespace = instantiateNamespaceFromXml(namespaceDocument
110: .getNamespace());
111: context.getCommonRepository().fireRepositoryEvent(
112: RepositoryEventType.NAMESPACE_REGISTERED,
113: new Long(namespace.getId()), 0);
114:
115: return namespace;
116: }
117:
118: public NamespaceImpl registerNamespace(String namespaceName,
119: AuthenticatedUser user) throws RepositoryException {
120: return registerNamespace(namespaceName, null, user);
121: }
122:
123: public NamespaceImpl unregisterNamespace(String namespaceName,
124: AuthenticatedUser user) throws RepositoryException {
125: DaisyHttpClient httpClient = getClient(user);
126:
127: String encodedName = encodeNameForUseInPath("namespace",
128: namespaceName);
129: HttpMethod method = new DeleteMethod(
130: "/repository/namespaceByName/" + encodedName);
131:
132: NamespaceDocument namespaceDocument = (NamespaceDocument) httpClient
133: .executeMethod(method, NamespaceDocument.class, true);
134: NamespaceImpl namespace = instantiateNamespaceFromXml(namespaceDocument
135: .getNamespace());
136:
137: context.getCommonRepository().fireRepositoryEvent(
138: RepositoryEventType.NAMESPACE_UNREGISTERED,
139: new Long(namespace.getId()), 0);
140:
141: return namespace;
142: }
143:
144: public NamespaceImpl unregisterNamespace(long id,
145: AuthenticatedUser user) throws RepositoryException {
146: DaisyHttpClient httpClient = getClient(user);
147:
148: HttpMethod method = new DeleteMethod("/repository/namespace/"
149: + id);
150:
151: NamespaceDocument namespaceDocument = (NamespaceDocument) httpClient
152: .executeMethod(method, NamespaceDocument.class, true);
153: NamespaceImpl namespace = instantiateNamespaceFromXml(namespaceDocument
154: .getNamespace());
155:
156: context.getCommonRepository().fireRepositoryEvent(
157: RepositoryEventType.NAMESPACE_UNREGISTERED,
158: new Long(namespace.getId()), 0);
159:
160: return namespace;
161: }
162:
163: public Namespace[] getAllNamespaces(AuthenticatedUser user)
164: throws RepositoryException {
165: DaisyHttpClient httpClient = getClient(user);
166: HttpMethod method = new GetMethod("/repository/namespace");
167:
168: NamespacesDocument namespacesDocument = (NamespacesDocument) httpClient
169: .executeMethod(method, NamespacesDocument.class, true);
170: List<NamespaceDocument.Namespace> namespacesXml = namespacesDocument
171: .getNamespaces().getNamespaceList();
172:
173: NamespaceImpl[] namespaces = new NamespaceImpl[namespacesXml
174: .size()];
175: for (int i = 0; i < namespacesXml.size(); i++) {
176: namespaces[i] = instantiateNamespaceFromXml(namespacesXml
177: .get(i));
178: }
179: return namespaces;
180: }
181:
182: private NamespaceImpl instantiateNamespaceFromXml(
183: NamespaceDocument.Namespace namespaceXml) {
184: return new NamespaceImpl(namespaceXml.getId(), namespaceXml
185: .getName(), namespaceXml.getFingerprint(), namespaceXml
186: .getRegisteredBy(), namespaceXml.getRegisteredOn()
187: .getTime());
188: }
189:
190: public Namespace getNamespaceByName(String namespaceName,
191: AuthenticatedUser user) throws RepositoryException {
192: DaisyHttpClient httpClient = getClient(user);
193:
194: String encodedName = encodeNameForUseInPath("namespace",
195: namespaceName);
196: HttpMethod method = new GetMethod(
197: "/repository/namespaceByName/" + encodedName);
198:
199: NamespaceDocument namespaceDocument = (NamespaceDocument) httpClient
200: .executeMethod(method, NamespaceDocument.class, true);
201: return instantiateNamespaceFromXml(namespaceDocument
202: .getNamespace());
203: }
204:
205: public String getRepositoryNamespace(AuthenticatedUser user)
206: throws RepositoryException {
207: DaisyHttpClient httpClient = getClient(user);
208: HttpMethod method = new GetMethod("/repository/namespace");
209:
210: NamespacesDocument namespacesDocument = (NamespacesDocument) httpClient
211: .executeMethod(method, NamespacesDocument.class, true);
212: return namespacesDocument.getNamespaces()
213: .getRepositoryNamespace();
214: }
215: }
|