KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > clientimpl > infrastructure > AbstractRemoteStrategy


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.infrastructure;
17
18 import org.outerj.daisy.repository.clientimpl.RemoteRepositoryManager;
19 import org.outerj.daisy.repository.commonimpl.AuthenticatedUser;
20 import org.outerj.daisy.repository.RepositoryException;
21 import org.apache.commons.httpclient.UsernamePasswordCredentials;
22 import org.apache.commons.httpclient.URIException;
23 import org.apache.commons.httpclient.NameValuePair;
24 import org.apache.commons.httpclient.HttpState;
25 import org.apache.commons.httpclient.util.URIUtil;
26
27 public class AbstractRemoteStrategy {
28     protected RemoteRepositoryManager.Context context;
29
30     public AbstractRemoteStrategy(RemoteRepositoryManager.Context context) {
31         this.context = context;
32     }
33
34     protected DaisyHttpClient getClient(AuthenticatedUser user) {
35         HttpState httpState = new HttpState();
36         httpState.setAuthenticationPreemptive(true);
37         // @'s in the login should be escaped by doubling them
38
String JavaDoc login = user.getLogin().replaceAll("@", "@@");
39         UsernamePasswordCredentials credentials =
40                 new UsernamePasswordCredentials(login + "@" + getActiveRoleString(user.getActiveRoleIds()), user.getPassword());
41         httpState.setCredentials(null, null, credentials);
42
43         DaisyHttpClient httpClient = new DaisyHttpClient(context.getSharedHttpClient(), context.getSharedHostConfiguration(), httpState);
44         return httpClient;
45     }
46
47     private String JavaDoc getActiveRoleString(long[] activeRoleIds) {
48         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(activeRoleIds.length * 4);
49         for (int i = 0; i < activeRoleIds.length; i++) {
50             if (i > 0)
51                 buffer.append(',');
52             buffer.append(activeRoleIds[i]);
53         }
54         return buffer.toString();
55     }
56
57     protected static final String JavaDoc encodeNameForUseInPath(String JavaDoc name, String JavaDoc value) throws RepositoryException {
58         try {
59             return URIUtil.encodeWithinPath(value);
60         } catch (URIException e) {
61             throw new RepositoryException("Error encoding " + name + " string", e);
62         }
63     }
64
65     protected NameValuePair[] getBranchLangParams(long branchId, long languageId) {
66         NameValuePair[] queryString = new NameValuePair[2];
67         queryString[0] = new NameValuePair("branch", String.valueOf(branchId));
68         queryString[1] = new NameValuePair("language", String.valueOf(languageId));
69         return queryString;
70     }
71 }
72
Popular Tags