This repository has been archived by the owner on Dec 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTsq.java
224 lines (181 loc) · 6.87 KB
/
Tsq.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* Licensed Materials - Property of IBM */
/* */
/* SAMPLE */
/* */
/* (c) Copyright IBM Corp. 2016 All Rights Reserved */
/* */
/* US Government Users Restricted Rights - Use, duplication or disclosure */
/* restricted by GSA ADP Schedule Contract with IBM Corp */
/* */
package com.ibm.cics.wlp.devworks.jaxrs.web;
import java.util.ArrayList;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import com.ibm.cics.server.CicsConditionException;
import com.ibm.cics.server.examples.wlp.tsq.BrowseTsqOSGi;
import com.ibm.cics.server.examples.wlp.tsq.DeleteTsqOSGi;
import com.ibm.cics.server.examples.wlp.tsq.TsqInfoOSGi;
import com.ibm.cics.server.examples.wlp.tsq.WriteTsqOSGi;
import com.ibm.json.java.JSONArray;
import com.ibm.json.java.JSONObject;
@javax.ws.rs.Path("/tsq/{tsqName}")
public class Tsq {
/**
* The GET HTTP Method is used to get (or browse) a TSQ record. We receive
* the name of the tsq to browse in the URI string.
*
* @param tsqName
* - The name of the TSQ to browse
* @return The TSQ record requested in the URI as a JSONObject i.e
* {"records"
* :[{"record":"RECORD1"},{"record":"RECORD2"}],"tsqName":"TSQ"}
* @throws Exception
*/
@javax.ws.rs.GET
@Produces("application/json")
public JSONObject browseTsq(@javax.ws.rs.PathParam("tsqName") String tsqName) throws Exception {
// create the object that will interact with the TSQ
BrowseTsqOSGi browseTsq;
browseTsq = new BrowseTsqOSGi();
// create an ArrayList, insert the TSQ records
ArrayList<String> records = new ArrayList<String>();
try {
records = browseTsq.browseTSQ(tsqName);
} catch (Exception e) {
if (e instanceof com.ibm.cics.server.InvalidQueueIdException){
// Carry on if the queue is empty
} else {
throw e;
}
}
// Put each record into a JSONObject and add it to a JSONArray
JSONArray queueArray = new JSONArray();
for (String record : records) {
JSONObject jsonRecord = new JSONObject();
jsonRecord.put("record", record);
queueArray.add(jsonRecord);
}
// create an JSONObject to return
JSONObject tsq = new JSONObject();
// put the name of the TSQ
tsq.put("tsqName", tsqName);
// put the array of records
tsq.put("records", queueArray);
// return the object
return tsq;
}
/**
* A HTTP PUT Method is usually only used to update a resource so we will
* use this method to update/write to an existing TSQ.
*
* @return The result of writing to an existing TSQ in a JSON Object i.e
* {result:"success or failure",tsqName:"TSQ"}
* @throws Exception
*/
@javax.ws.rs.PUT
@Consumes("application/json")
@Produces("application/json")
public JSONObject updateTsq(JSONObject jsonTsq) throws Exception {
// string to hold the result of writing to the tsq
String result = "";
// Get the name of the TSQ to write to
String tsqName = (String) jsonTsq.get("tsqName");
// Create the object to see if the TSQ exists
// and fetch the number of items in the queue
TsqInfoOSGi tsqInfo = new TsqInfoOSGi(tsqName);
int items = tsqInfo.getTSQLength();
if (items > 0) {
// The TSQ has existing items, get the record to be added from
// the JSONObject
String record = (String) jsonTsq.get("record");
// Create the object that will actually write to the TSQ
WriteTsqOSGi writeTsq = new WriteTsqOSGi();
// write the record to the TSQ and store the result
result = writeTsq.writeTSQ(tsqName, record);
} else {
result = "You are trying to write to an existing queue but TSQ "
+ tsqName
+ " does not exist. Try creating a TSQ using a POST method instead.";
}
// create the result JSONObject
JSONObject jsonResult = new JSONObject();
// put the tsq name in the object
jsonResult.put("tsqName", tsqName);
// put the result in the object
jsonResult.put("result", result);
// return the object
return jsonResult;
}
/**
* The HTTP POST Method is used to create a new resource at the given URI.
* We will only create a new TSQ when POST is used. Client supplies
* JSONObject containing the TSQ name and record data which is then written
* to a queue.
*
* @return The result of creating a new TSQ and the name of the new queue as
* a JSONObject i.e {result:"result from create",tsqName:"TSQ"}
* @throws Exception
*/
@javax.ws.rs.POST
@Consumes("application/json")
@Produces("application/json")
public JSONObject createTsq(JSONObject jsonTsq) throws Exception {
// a string to store the result
String result = "";
// Get the name of the TSQ to write to
String tsqName = (String) jsonTsq.get("tsqName");
// Create the object to see if the TSQ exists
// and fetch the number of items in the queue
TsqInfoOSGi tsqInfo = new TsqInfoOSGi(tsqName);
int items = tsqInfo.getTSQLength();
if (items > 0) {
result = "You are trying to create a TSQ but " + tsqName
+ " already exists,"
+ "try using a PUT method to write to an existing TSQ";
} else {
// no items found so TSQ does not exist
// get the record from the JSONObject to be written to the TSQ
String record = (String) jsonTsq.get("record");
// Create the object that will actually write to the TSQ
WriteTsqOSGi writeTsq = new WriteTsqOSGi();
// write the record to the tsq and store the result
result = writeTsq.writeTSQ(tsqName, record);
}
// create the result JSONObject
JSONObject jsonResult = new JSONObject();
// put the tsq name in the object
jsonResult.put("tsqName", tsqName);
// put the result in the object
jsonResult.put("result", result);
// return the object
return jsonResult;
}
/**
*
* delete the TSQ corresponding to the name received in the URI.
*
* @param tsqName
* - the name of the TSQ to DELETE.
*
* @return The result of the DELETE as a JSONObject i.e
* {result:"result of delete",tsqName:"TSQ"}
* @throws Exception
*/
@javax.ws.rs.DELETE
@Produces("application/json")
public JSONObject deleteTsq(@javax.ws.rs.PathParam("tsqName") String tsqName) throws Exception {
String result = "";
// create the object that will actually delete the TSQ
DeleteTsqOSGi deleteTsq = new DeleteTsqOSGi();
// delete the TSQ and store the result
result = deleteTsq.deleteTSQ(tsqName);
// create the result JSONObject
JSONObject jsonResult = new JSONObject();
// put the tsq name in the object
jsonResult.put("tsqName", tsqName);
// put the result in the object
jsonResult.put("result", result);
// return the object
return jsonResult;
}
}