1 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 String 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 getActiveRoleString(long[] activeRoleIds) { 48 StringBuffer buffer = new StringBuffer (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 encodeNameForUseInPath(String name, String 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 |