1 package nl.hippo.cms.workflows.multiplesitesdocument; 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.Iterator ; 8 import java.util.Map ; 9 import nl.hippo.cms.repositorylocation.CommonRepositoryLocationRoles; 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.HttpClient; 14 import org.apache.commons.httpclient.HttpState; 15 import org.apache.commons.httpclient.methods.GetMethod; 16 import org.apache.commons.httpclient.methods.PutMethod; 17 18 public class CopyObjectToPreviewRepositoriesFunction extends ObjectCopyingComponent 19 { 20 21 public CopyObjectToPreviewRepositoriesFunction() 22 { 23 super(); 24 } 25 26 public void executeImpl(final Map transientVars, Map args, PropertySet ps) 27 throws WorkflowException 28 { 29 GetMethod get = null; 30 PutMethod put = null; 31 try 32 { 33 RepositoryLocation editorRepo = (RepositoryLocation) m_manager 34 .lookup(CommonRepositoryLocationRoles.EDITOR_REPOSITORY_LOCATION_ROLE); 35 try 36 { 37 String editorLocation = editorRepo.getRepositoryInformation().getAbsoluteUri( 38 ps.getString(MultipleSitesDocumentConstants.LOCATION_KEY)); 39 SitesDirectory sitesDir = (SitesDirectory) m_manager.lookup(SitesDirectory.ROLE); 40 try 41 { 42 Iterator siteIdsIter = sitesDir.siteIdsIterator(); 43 while (siteIdsIter.hasNext()) 44 { 45 String siteId = (String ) siteIdsIter.next(); 46 String previewLocation = sitesDir.getSite(siteId).getPreviewRepository() 47 .getAbsoluteUri(ps.getString(MultipleSitesDocumentConstants.LOCATION_KEY)); 48 HttpState httpState = (HttpState) transientVars.get("httpstate"); 49 HttpClient httpClient = new HttpClient(); 50 httpClient.setState(httpState); 51 get = new GetMethod(editorLocation); 52 get.setDoAuthentication(true); 53 int getResult = httpClient.executeMethod(get); 54 byte[] body = get.getResponseBody(); 56 get.releaseConnection(); 57 58 ensureAncestorCollectionsExist(httpClient, previewLocation); 59 60 put = new PutMethod(previewLocation); 61 put.setDoAuthentication(true); 62 put.setRequestBody(new ByteArrayInputStream (body)); 63 int putResult = httpClient.executeMethod(put); 64 put.releaseConnection(); 66 67 copyProperties(httpClient, editorLocation, previewLocation); 68 } 69 } 70 finally 71 { 72 m_manager.release(sitesDir); 73 } 74 } 75 finally 76 { 77 m_manager.release(editorRepo); 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 } | Popular Tags |