KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nl > hippo > cms > workflows > multiplesitessite > CopyVersionToLiveRepositoryFunction


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 JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.util.Map JavaDoc;
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 JavaDoc transientVars, Map JavaDoc 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 JavaDoc relativeUri = ps.getString(MultipleSitesSiteConstants.LOCATION_KEY);
40                 RepositoryInformation historyRepoInformation = historyRepo.getRepositoryInformation();
41                 String JavaDoc historyLocation = historyRepoInformation.getAbsoluteUri(
42                         relativeUri + "/" + transientVars.get("versionToPublish") + ".xml");
43                 SitesDirectory sitesDir = (SitesDirectory) m_manager.lookup(SitesDirectory.ROLE);
44                 try
45                 {
46                     String JavaDoc siteId = ps.getString(MultipleSitesSiteConstants.SITE_ID_KEY);
47                     RepositoryInformation liveRepoInformation = sitesDir.getSite(siteId).getLiveRepository();
48                     String JavaDoc 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                     // TODO: handle result
55
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 JavaDoc(body));
64                     int putResult = liveHttpClient.executeMethod(put);
65                     // TODO: handle result
66
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 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     private HttpClient createHttpClient(RepositoryInformation repoInformation)
102     {
103         HttpClient result = new HttpClient();
104         
105         HttpState httpState = new HttpState();
106         String JavaDoc username = repoInformation.getUsername();
107         String JavaDoc 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