1
2
3
4
5
6
7
8
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
27
28
29
30 public class RestfulStructureGWES extends RestfulGWES implements IStructureService {
31
32 final static Logger logger = Logger.getLogger(RestfulStructureGWES.class);
33
34
35
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
49
50
51
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
65
66
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
79
80
81
82
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
90
91 StringBuffer urlBuf = new StringBuffer(gwesServiceUrl);
92 urlBuf.append(METHOD_COMMITMODIFICATION);
93 StringBuffer payload = new StringBuffer();
94
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
101 payload.append(STRING_LSLASH).append(METHOD_COMMITMODIFICATION).append(STRING_G);
102
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
122 return httpGETArray(urlBuf.toString());
123 }
124
125 }