View Javadoc

1   /*
2    * $Id: RestfulStructureGWES.java 1537 2011-07-27 15:34:04Z hoheisel $
3    *
4    * Copyright 2008 Fraunhofer Gesellschaft, Munich, Germany,
5    * for its Fraunhofer Institute for Computer Architecture and Software Technology (FIRST), Berlin, Germany
6    * All rights reserved.
7    *
8    * See http://www.first.fraunhofer.de and http://www.gridworkflow.org/gwes for more details.
9    */
10  
11  package net.kwfgrid.gwes.restfulclient;
12  
13  import net.kwfgrid.gworkflowdl.protocol.service.IStructureService;
14  import net.kwfgrid.gworkflowdl.protocol.IllegalModificationException;
15  import net.kwfgrid.gworkflowdl.protocol.IncompatibleVersionsException;
16  import net.kwfgrid.gwes.util.StringUtils;
17  import net.kwfgrid.gwes.util.HTMLFilter;
18  import net.kwfgrid.gwes.Constants;
19  
20  import java.rmi.RemoteException;
21  import java.util.Hashtable;
22  
23  import org.apache.log4j.Logger;
24  
25  /**
26   * @author Andreas Hoheisel
27   *         (<a href="http://www.andreas-hoheisel.de">www.andreas-hoheisel.de</a>)
28   * @version $Id: RestfulStructureGWES.java 1537 2011-07-27 15:34:04Z hoheisel $
29   */
30  public class RestfulStructureGWES extends RestfulGWES implements IStructureService {
31  
32      final static Logger logger = Logger.getLogger(RestfulStructureGWES.class);
33  
34      /**
35       * Key is gwesServiceUrl, value is RestfulStructureGWES.
36       */
37      private static Hashtable<String,RestfulStructureGWES> instances = new Hashtable<String,RestfulStructureGWES>();
38  
39      private static final String METHOD_COMMITMODIFICATION = "commitModification";
40      private static final String METHOD_GETMODIFICATIONSFORUPDATE = "getModificationsForUpdate";
41  
42      private static final String ELEMENT_CLIENTWORKFLOWVERSION = "clientWorkflowVersion";
43      private static final String ELEMENT_MODIFICATION = "modification";
44  
45      private static final String _PARAM_CLIENTWORKFLOWVERSION = "&clientWorkflowVersion=";
46  
47      /**
48       * Create new or return existing RestfulStructureGWES instance to GWES with service URL as specified by system property.
49       * @see net.kwfgrid.gwes.Constants#PROP_SYSTEM_GWES_SERVICE_BASE_URL_INTERNAL
50       * @return
51       * @throws Exception
52       */
53      public synchronized static RestfulStructureGWES getInstance() throws Exception {
54          String gwesServiceUrl = System.getProperty(Constants.PROP_SYSTEM_GWES_SERVICE_BASE_URL_INTERNAL) + "/services/GWES";
55          if (gwesServiceUrl.equals("null/services/GWES")) {
56              throw new Exception("The GWES client has not been configured correctly. Please set the property "
57                      + Constants.PROP_SYSTEM_GWES_SERVICE_BASE_URL_INTERNAL + " in the file gwes.properties or as system property");
58          }
59  
60          return getInstance(gwesServiceUrl);
61      }
62  
63      /**
64       * Create new or return existing RestfulStructureGWES instance to GWES with service URL as specified by paramter gwesServiceUrl.
65       * @return
66       * @throws Exception
67       */
68      public synchronized static RestfulStructureGWES getInstance(String gwesServiceUrl) throws Exception {
69          RestfulStructureGWES _instance = instances.get(gwesServiceUrl);
70          if (_instance == null) {
71              _instance = new RestfulStructureGWES(gwesServiceUrl);
72              instances.put(gwesServiceUrl,_instance);
73          }
74          return _instance;
75      }
76  
77      /**
78       * Private constructor for internal use only.
79       * Use getInstance() methods instead.
80       *
81       * @param gwesServiceUrl
82       * @throws Exception
83       */
84      private RestfulStructureGWES(String gwesServiceUrl) throws Exception {
85          super(gwesServiceUrl);
86      }
87  
88      public int commitModification(String structureid, int clientversion, String modification, String userID) throws IllegalModificationException, IncompatibleVersionsException, RemoteException {
89          // ToDo: throw IllegalModificationException
90          // ToDo: throw IncompatibleVersionsException
91          StringBuffer urlBuf = new StringBuffer(gwesServiceUrl);
92          urlBuf.append(METHOD_COMMITMODIFICATION);
93          StringBuffer payload = new StringBuffer();
94          // <commitModification xmlns="http://gwes.kwfgrid.net">
95          payload.append(STRING_L).append(METHOD_COMMITMODIFICATION).append(ATTRIBUTE_NS).append(STRING_G);
96          StringUtils.addXmlToStringBuffer(payload,ELEMENT_WORKFLOWID, structureid);
97          StringUtils.addXmlToStringBuffer(payload,ELEMENT_CLIENTWORKFLOWVERSION, clientversion);
98          StringUtils.addXmlToStringBuffer(payload,ELEMENT_MODIFICATION, HTMLFilter.filter(modification));
99          StringUtils.addXmlToStringBuffer(payload,ELEMENT_USERID, userID);
100         // </commitModification>
101         payload.append(STRING_LSLASH).append(METHOD_COMMITMODIFICATION).append(STRING_G);
102         // PUT
103         String[] response = httpPUT(urlBuf.toString(), payload.toString());
104         if (response != null && response.length > 0) {
105             try {
106                 return Integer.parseInt(response[0]);
107             } catch (NumberFormatException e) {
108                 throw new RemoteException("Exception during commitModification(): "+e,e);
109             }
110         } else {
111             throw new RemoteException("GWES did not return a value for HTTP PUT "+urlBuf.toString());
112         }
113     }
114 
115     public String[][] getModificationsForUpdate(String structureid, int clientversion, String userID) throws RemoteException {
116         StringBuffer urlBuf = new StringBuffer(gwesServiceUrl);
117         urlBuf.append(METHOD_GETMODIFICATIONSFORUPDATE);
118         urlBuf.append(PARAM_WORKFLOWID).append(structureid);
119         urlBuf.append(_PARAM_CLIENTWORKFLOWVERSION).append(clientversion);
120         urlBuf.append(_PARAM_USERID).append(userID);
121         // GET
122         return httpGETArray(urlBuf.toString());
123     }
124 
125 }