KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > clientimpl > RemoteRepositoryImpl


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.repository.clientimpl;
17
18 import org.outerj.daisy.repository.commonimpl.RepositoryImpl;
19 import org.outerj.daisy.repository.commonimpl.CommonRepository;
20 import org.outerj.daisy.repository.commonimpl.AuthenticatedUser;
21 import org.outerj.daisy.repository.RepositoryException;
22 import org.outerj.daisy.repository.clientimpl.infrastructure.AbstractRemoteStrategy;
23 import org.outerj.daisy.repository.clientimpl.infrastructure.DaisyHttpClient;
24 import org.apache.commons.httpclient.methods.GetMethod;
25 import org.apache.commons.httpclient.NameValuePair;
26
27 import java.util.Map JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 public class RemoteRepositoryImpl extends RepositoryImpl {
31     private RemoteClient remoteClient;
32     private RemoteRepositoryManager.Context context;
33
34     public RemoteRepositoryImpl(CommonRepository delegate, AuthenticatedUser user, RemoteRepositoryManager.Context context) {
35         super(delegate, user);
36         this.context = context;
37     }
38
39     /**
40      * Communicate to the daisy server using HTTP. Advantage of using this method
41      * (instead of doing it just yourself) is that the authentication information
42      * will be automatically passed on, and if the result is an error appropriate
43      * exceptions will be thrown.
44      *
45      * <p>The performed request is a GET request.
46      *
47      */

48     public GetMethod getResource(String JavaDoc path, Map JavaDoc parameters) throws RepositoryException {
49         if (remoteClient == null)
50             remoteClient = new RemoteClient(context);
51
52         GetMethod getMethod = new GetMethod(path);
53         NameValuePair[] queryString = new NameValuePair[parameters.size()];
54         int i = 0;
55         Iterator JavaDoc parametersIt = parameters.entrySet().iterator();
56         while (parametersIt.hasNext()) {
57             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)parametersIt.next();
58             String JavaDoc key = (String JavaDoc)entry.getKey();
59             String JavaDoc value = entry.getValue().toString();
60             queryString[i] = new NameValuePair(key, value);
61             i++;
62         }
63         getMethod.setQueryString(queryString);
64
65         DaisyHttpClient httpClient = remoteClient.getHttpClientForCurrentUser();
66         httpClient.executeMethod(getMethod, null, false);
67
68         return getMethod;
69     }
70
71     public DaisyHttpClient getHttpClient() {
72         if (remoteClient == null)
73             remoteClient = new RemoteClient(context);
74
75         DaisyHttpClient httpClient = remoteClient.getHttpClientForCurrentUser();
76         return httpClient;
77     }
78
79     public String JavaDoc getBaseURL() {
80         return context.getBaseURL();
81     }
82
83     private class RemoteClient extends AbstractRemoteStrategy {
84         public RemoteClient(RemoteRepositoryManager.Context context) {
85             super(context);
86         }
87
88         protected DaisyHttpClient getHttpClientForCurrentUser() {
89             return getClient(user);
90         }
91     }
92
93     public Object JavaDoc clone() {
94         return new RemoteRepositoryImpl(getCommonRepository(), getUser(), context);
95     }
96 }
97
Popular Tags