1 package nl.hippo.cms.workflows.multiplesitessite; 2 3 import com.opensymphony.module.propertyset.PropertySet; 4 import com.opensymphony.workflow.WorkflowException; 5 import java.io.ByteArrayInputStream ; 6 import java.io.IOException ; 7 import java.util.Map ; 8 import nl.hippo.cms.repositorylocation.CommonRepositoryLocationRoles; 9 import nl.hippo.cms.repositorylocation.RepositoryInformation; 10 import nl.hippo.cms.repositorylocation.RepositoryLocation; 11 import nl.hippo.cms.sitesdirectory.SitesDirectory; 12 import org.apache.avalon.framework.service.ServiceException; 13 import org.apache.commons.httpclient.Credentials; 14 import org.apache.commons.httpclient.HttpClient; 15 import org.apache.commons.httpclient.HttpState; 16 import org.apache.commons.httpclient.UsernamePasswordCredentials; 17 import org.apache.commons.httpclient.methods.GetMethod; 18 import org.apache.commons.httpclient.methods.PutMethod; 19 20 public class CopyVersionToLiveRepositoryFunction extends ObjectCopyingComponent 21 { 22 23 public CopyVersionToLiveRepositoryFunction() 24 { 25 super(); 26 } 27 28 public void executeImpl(Map transientVars, Map args, PropertySet ps) 29 throws WorkflowException 30 { 31 GetMethod get = null; 32 PutMethod put = null; 33 try 34 { 35 RepositoryLocation historyRepo = (RepositoryLocation) m_manager 36 .lookup(CommonRepositoryLocationRoles.HISTORY_REPOSITORY_LOCATION_ROLE); 37 try 38 { 39 String relativeUri = ps.getString(MultipleSitesSiteConstants.LOCATION_KEY); 40 RepositoryInformation historyRepoInformation = historyRepo.getRepositoryInformation(); 41 String historyLocation = historyRepoInformation.getAbsoluteUri( 42 relativeUri + "/" + transientVars.get("versionToPublish") + ".xml"); 43 SitesDirectory sitesDir = (SitesDirectory) m_manager.lookup(SitesDirectory.ROLE); 44 try 45 { 46 String siteId = ps.getString(MultipleSitesSiteConstants.SITE_ID_KEY); 47 RepositoryInformation liveRepoInformation = sitesDir.getSite(siteId).getLiveRepository(); 48 String liveLocation = liveRepoInformation 49 .getAbsoluteUri(relativeUri); 50 HttpClient historyHttpClient = createHttpClient(historyRepoInformation); 51 get = new GetMethod(historyLocation); 52 get.setDoAuthentication(true); 53 int getResult = historyHttpClient.executeMethod(get); 54 byte[] body = get.getResponseBody(); 56 get.releaseConnection(); 57 58 HttpClient liveHttpClient = createHttpClient(liveRepoInformation); 59 ensureAncestorCollectionsExist(historyHttpClient, historyRepoInformation, liveHttpClient, liveRepoInformation, relativeUri); 60 61 put = new PutMethod(liveLocation); 62 put.setDoAuthentication(true); 63 put.setRequestBody(new ByteArrayInputStream (body)); 64 int putResult = liveHttpClient.executeMethod(put); 65 put.releaseConnection(); 67 68 copyProperties(historyHttpClient, historyLocation, liveHttpClient, liveLocation); 69 } 70 finally 71 { 72 m_manager.release(sitesDir); 73 } 74 } 75 finally 76 { 77 m_manager.release(historyRepo); 78 } 79 } 80 catch (IOException e) 81 { 82 throw new WorkflowException(e); 83 } 84 catch (ServiceException e) 85 { 86 throw new WorkflowException(e); 87 } 88 finally 89 { 90 if (get != null) 91 { 92 get.releaseConnection(); 93 } 94 if (put != null) 95 { 96 put.releaseConnection(); 97 } 98 } 99 } 100 101 private HttpClient createHttpClient(RepositoryInformation repoInformation) 102 { 103 HttpClient result = new HttpClient(); 104 105 HttpState httpState = new HttpState(); 106 String username = repoInformation.getUsername(); 107 String password = repoInformation.getPassword(); 108 Credentials credentials = new UsernamePasswordCredentials(username, password); 109 httpState.setCredentials(repoInformation.getRealm(), repoInformation.getHost(), credentials); 110 result.setState(httpState); 111 112 return result; 113 } 114 115 } | Popular Tags |