KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nl > hippo > cms > workflows > multiplesitesdocument > CopyObjectToPreviewRepositoriesFunction


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 JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.Map JavaDoc;
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 JavaDoc transientVars, Map JavaDoc 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 JavaDoc editorLocation = editorRepo.getRepositoryInformation().getAbsoluteUri(
38                         ps.getString(MultipleSitesDocumentConstants.LOCATION_KEY));
39                 SitesDirectory sitesDir = (SitesDirectory) m_manager.lookup(SitesDirectory.ROLE);
40                 try
41                 {
42                     Iterator JavaDoc siteIdsIter = sitesDir.siteIdsIterator();
43                     while (siteIdsIter.hasNext())
44                     {
45                         String JavaDoc siteId = (String JavaDoc) siteIdsIter.next();
46                         String JavaDoc 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                         // TODO: handle result
55
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 JavaDoc(body));
63                         int putResult = httpClient.executeMethod(put);
64                         // TODO: handle result
65
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 JavaDoc 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