Port champ-microservice project restructure
[aai/champ.git] / champ-service / src / main / java / org / onap / champ / ChampRESTAPI.java
1 package org.onap.champ;
2
3 import java.io.IOException;
4 import java.util.HashMap;
5 import java.util.HashSet;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Optional;
9 import java.util.Timer;
10
11 import javax.servlet.http.HttpServletRequest;
12 import javax.ws.rs.Consumes;
13 import javax.ws.rs.DELETE;
14 import javax.ws.rs.GET;
15 import javax.ws.rs.POST;
16 import javax.ws.rs.PUT;
17 import javax.ws.rs.Path;
18 import javax.ws.rs.PathParam;
19 import javax.ws.rs.Produces;
20 import javax.ws.rs.QueryParam;
21 import javax.ws.rs.core.Context;
22 import javax.ws.rs.core.HttpHeaders;
23 import javax.ws.rs.core.MediaType;
24 import javax.ws.rs.core.Response;
25 import javax.ws.rs.core.Response.Status;
26 import javax.ws.rs.core.UriInfo;
27
28 import org.json.JSONException;
29 import org.json.JSONObject;
30 import org.onap.aai.champcore.ChampTransaction;
31 import org.onap.aai.champcore.exceptions.ChampObjectNotExistsException;
32 import org.onap.aai.champcore.exceptions.ChampRelationshipNotExistsException;
33 import org.onap.aai.champcore.exceptions.ChampTransactionException;
34 import org.onap.aai.champcore.exceptions.ChampUnmarshallingException;
35 import org.onap.aai.champcore.model.ChampObject;
36 import org.onap.aai.champcore.model.ChampRelationship;
37 import org.onap.aai.cl.api.Logger;
38 import org.onap.aai.cl.eelf.LoggerFactory;
39 import org.onap.champ.async.ChampAsyncRequestProcessor;
40 import org.onap.champ.entity.ChampObjectDeserializer;
41 import org.onap.champ.entity.ChampObjectSerializer;
42 import org.onap.champ.entity.ChampRelationshipDeserializer;
43 import org.onap.champ.entity.ChampRelationshipSerializer;
44 import org.onap.champ.exception.ChampServiceException;
45 import org.onap.champ.service.ChampDataService;
46 import org.onap.champ.service.logging.ChampMsgs;
47 import org.onap.champ.service.logging.LoggingUtil;
48 import org.onap.champ.util.ChampProperties;
49 import org.onap.champ.util.ChampServiceConstants;
50
51 import com.fasterxml.jackson.core.JsonProcessingException;
52 import com.fasterxml.jackson.databind.ObjectMapper;
53 import com.fasterxml.jackson.databind.module.SimpleModule;
54
55 @Path(value = "/")
56 public class ChampRESTAPI {
57
58   private ObjectMapper mapper;
59
60   private ChampDataService champDataService;
61   private String TRANSACTION_METHOD = "method";
62   private Timer timer;
63
64   private Logger logger = LoggerFactory.getInstance().getLogger(ChampRESTAPI.class);
65   Logger auditLogger = LoggerFactory.getInstance().getAuditLogger(ChampRESTAPI.class.getName());
66   private static Logger metricsLogger = LoggerFactory.getInstance().getMetricsLogger(ChampRESTAPI.class.getName());
67
68   public ChampRESTAPI(ChampDataService champDataService, ChampAsyncRequestProcessor champAsyncRequestProcessor) {
69     this.champDataService = champDataService;
70
71     // Async request handling is optional.  
72     if (champAsyncRequestProcessor != null) {
73       timer = new Timer("ChampAsyncRequestProcessor-1");
74       timer.schedule(champAsyncRequestProcessor, champAsyncRequestProcessor.getRequestPollingTimeSeconds(),
75           champAsyncRequestProcessor.getRequestPollingTimeSeconds());
76     }
77
78     mapper = new ObjectMapper();
79     SimpleModule module = new SimpleModule();
80     module.addSerializer(ChampObject.class, new ChampObjectSerializer());
81     module.addDeserializer(ChampObject.class, new ChampObjectDeserializer());
82     module.addSerializer(ChampRelationship.class, new ChampRelationshipSerializer());
83     module.addDeserializer(ChampRelationship.class, new ChampRelationshipDeserializer());
84     mapper.registerModule(module);
85   }
86
87   @GET
88   @Path("echo")
89   @Produces(MediaType.TEXT_PLAIN)
90   public Response echo() {
91     return Response.ok().entity("alive").build();
92   }
93
94   @GET
95   @Path("objects/{objectId}")
96   @Produces(MediaType.APPLICATION_JSON)
97   public Response getObject(@PathParam("objectId") String objectId, @QueryParam("transactionId") String tId,
98       @Context HttpHeaders headers, @Context UriInfo uriInfo, @Context HttpServletRequest req) {
99     LoggingUtil.initMdcContext(req, headers);
100     long startTimeInMs = System.currentTimeMillis();
101     logger.info(ChampMsgs.INCOMING_REQUEST, tId, objectId);
102
103     Response response = null;
104     ChampObject retrieved;
105
106     try {
107       ChampTransaction transaction = champDataService.getTransaction(tId);
108
109       if (tId != null && transaction == null) {
110         throw new ChampServiceException("transactionId not found", Status.BAD_REQUEST);
111       }
112       retrieved = champDataService.getObject(objectId, Optional.ofNullable(transaction));
113       if (retrieved == null) {
114         response = Response.status(Status.NOT_FOUND).entity(objectId + " not found").build();
115       } else {
116         response = Response.status(Status.OK).entity(mapper.writeValueAsString(retrieved)).build();
117       }
118
119     } catch (JsonProcessingException e) {
120       response = Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
121     } catch (ChampServiceException ce) {
122       response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
123     } finally {
124       logger.debug(response.getEntity().toString());
125       LoggingUtil.logRestRequest(logger, auditLogger, req, response);
126       metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "GET", Long.toString(System.currentTimeMillis() - startTimeInMs));
127     }
128
129     return response;
130   }
131
132   @DELETE
133   @Path("objects/{objectId}")
134   public Response deleteObject(@PathParam("objectId") String objectId, @QueryParam("transactionId") String tId,
135       @Context HttpHeaders headers, @Context UriInfo uriInfo, @Context HttpServletRequest req) {
136     LoggingUtil.initMdcContext(req, headers);
137     long startTimeInMs = System.currentTimeMillis();
138     logger.info(ChampMsgs.INCOMING_REQUEST, tId, objectId);
139     ChampObject retrieved;
140     Response response = null;
141     try {
142       ChampTransaction transaction = champDataService.getTransaction(tId);
143
144       if (tId != null && transaction == null) {
145         throw new ChampServiceException("transactionId not found", Status.BAD_REQUEST);
146       }
147       champDataService.deleteObject(objectId, Optional.ofNullable(transaction));
148
149       response = Response.status(Status.OK).build();
150     } catch (ChampObjectNotExistsException e) {
151       response = Response.status(Status.NOT_FOUND).entity(objectId + " not found").build();
152     } catch (ChampServiceException ce) {
153       response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
154     } catch (ChampTransactionException | ChampUnmarshallingException e) {
155       response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
156     } finally {
157       LoggingUtil.logRestRequest(logger, auditLogger, req, response);
158       metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "DELETE",
159           Long.toString(System.currentTimeMillis() - startTimeInMs));
160     }
161     return response;
162   }
163
164   @POST
165   @Path("objects")
166   @Consumes(MediaType.APPLICATION_JSON)
167   @Produces(MediaType.APPLICATION_JSON)
168   public Response postObject(String champObj, @QueryParam("transactionId") String tId, @Context HttpHeaders headers,
169       @Context UriInfo uriInfo, @Context HttpServletRequest req) {
170     LoggingUtil.initMdcContext(req, headers);
171     long startTimeInMs = System.currentTimeMillis();
172     logger.info(ChampMsgs.INCOMING_REQUEST, tId, champObj);
173     Response response = null;
174     try {
175       ChampTransaction transaction = champDataService.getTransaction(tId);
176       if (tId != null && transaction == null) {
177         throw new ChampServiceException("transactionId not found", Status.BAD_REQUEST);
178       }
179       ChampObject champObject = mapper.readValue(champObj, ChampObject.class);
180
181       ChampObject created = champDataService.storeObject(champObject, Optional.ofNullable(transaction));
182       response = Response.status(Status.CREATED).entity(mapper.writeValueAsString(created)).build();
183     } catch (IOException e) {
184       response = Response.status(Status.BAD_REQUEST).entity("Unable to parse the payload").build();
185     } catch (ChampServiceException ce) {
186       response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
187     } catch (Exception e) {
188       response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
189       LoggingUtil.logInternalError(logger, e);
190     } finally {
191       LoggingUtil.logRestRequest(logger, auditLogger, req, response);
192       metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "POST",
193           Long.toString(System.currentTimeMillis() - startTimeInMs));
194     }
195     return response;
196   }
197
198   @PUT
199   @Path("objects/{objectId}")
200   @Consumes(MediaType.APPLICATION_JSON)
201   @Produces(MediaType.APPLICATION_JSON)
202   public Response putObject(@PathParam("objectId") String objectId, String champObj,
203       @QueryParam("transactionId") String tId, @Context HttpHeaders headers, @Context UriInfo uriInfo,
204       @Context HttpServletRequest req) {
205     LoggingUtil.initMdcContext(req, headers);
206     long startTimeInMs = System.currentTimeMillis();
207     logger.info(ChampMsgs.INCOMING_REQUEST, tId, objectId + " " + champObj);
208
209     Response response = null;
210     try {
211       ChampTransaction transaction = champDataService.getTransaction(tId);
212       if (tId != null && transaction == null) {
213         throw new ChampServiceException("transactionId not found", Status.BAD_REQUEST);
214       }
215
216       ChampObject co = mapper.readValue(champObj, ChampObject.class);
217       // check if key is present or if it equals the key that is in the URI
218       ChampObject updated = champDataService.replaceObject(co, objectId, Optional.ofNullable(transaction));
219
220       response = Response.status(Status.OK).entity(mapper.writeValueAsString(updated)).build();
221     } catch (IOException e) {
222       response = Response.status(Status.BAD_REQUEST).entity("Unable to parse the payload").build();
223     } catch (ChampServiceException ce) {
224       response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
225     } catch (Exception e) {
226       response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
227       LoggingUtil.logInternalError(logger, e);
228     } finally {
229       LoggingUtil.logRestRequest(logger, auditLogger, req, response);
230       metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "PUT", Long.toString(System.currentTimeMillis() - startTimeInMs));
231     }
232     return response;
233   }
234
235   @GET
236   @Path("objects/relationships/{oId}")
237   @Produces(MediaType.APPLICATION_JSON)
238   public Response getEdges(@PathParam("oId") String oId, @QueryParam("transactionId") String tId,
239       @Context HttpHeaders headers, @Context UriInfo uriInfo, @Context HttpServletRequest req) {
240     LoggingUtil.initMdcContext(req, headers);
241     long startTimeInMs = System.currentTimeMillis();
242     List<ChampRelationship> retrieved;
243     Optional<ChampObject> rObject;
244     Response response = null;
245     ChampTransaction transaction = null;
246     try {
247
248       retrieved = champDataService.getRelationshipsByObject(oId, Optional.ofNullable(transaction));
249       response = Response.status(Status.OK).entity(mapper.writeValueAsString(retrieved)).build();
250     } catch (JsonProcessingException e) {
251       response = Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
252     } catch (ChampServiceException ce) {
253       response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
254     } catch (Exception e) {
255       response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
256       LoggingUtil.logInternalError(logger, e);
257     } finally {
258       LoggingUtil.logRestRequest(logger, auditLogger, req, response);
259       metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "GET", Long.toString(System.currentTimeMillis() - startTimeInMs));
260     }
261     return response;
262   }
263
264   @GET
265   @Path("objects/filter/")
266   @Produces(MediaType.APPLICATION_JSON)
267   public Response filterObject(@Context HttpHeaders headers, @Context UriInfo uriInfo,
268       @Context HttpServletRequest req) {
269     LoggingUtil.initMdcContext(req, headers);
270     long startTimeInMs = System.currentTimeMillis();
271     String propertiesKey = ChampProperties.get(ChampServiceConstants.CHAMP_COLLECTION_PROPERTIES_KEY);
272     List<ChampObject> objects;
273     Map<String, Object> filter = new HashMap<>();
274
275     for (Map.Entry<String, List<String>> e : uriInfo.getQueryParameters().entrySet()) {
276       if (!e.getKey().equals(propertiesKey)) {
277         filter.put(e.getKey(), e.getValue().get(0));
278       }
279     }
280
281     HashSet<String> properties;
282     if (uriInfo.getQueryParameters().containsKey(propertiesKey)) {
283       properties = new HashSet<>(uriInfo.getQueryParameters().get(propertiesKey));
284     } else {
285       properties = new HashSet<>();
286     }
287
288     Response response = null;
289     try {
290       objects = champDataService.queryObjects(filter, properties);
291       response = Response.status(Status.OK).type(MediaType.APPLICATION_JSON).entity(mapper.writeValueAsString(objects))
292           .build();
293     } catch (JsonProcessingException e) {
294       e.printStackTrace();
295       response = Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
296     } catch (ChampServiceException e1) {
297       response = Response.status(e1.getHttpStatus()).entity(e1.getMessage()).build();
298     } catch (Exception e) {
299       response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
300       LoggingUtil.logInternalError(logger, e);
301     } finally {
302       LoggingUtil.logRestRequest(logger, auditLogger, req, response);
303       metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "GET", Long.toString(System.currentTimeMillis() - startTimeInMs));
304     }
305     return response;
306   }
307
308   @GET
309   @Path("relationships/{rId}")
310   @Produces(MediaType.APPLICATION_JSON)
311   public Response getRelationship(@PathParam("rId") String rId, @QueryParam("transactionId") String tId,
312       @Context HttpHeaders headers, @Context UriInfo uriInfo, @Context HttpServletRequest req) {
313     LoggingUtil.initMdcContext(req, headers);
314     long startTimeInMs = System.currentTimeMillis();
315     logger.info(ChampMsgs.INCOMING_REQUEST, tId, rId);
316     ChampRelationship retrieved;
317     Response response = null;
318     try {
319       ChampTransaction transaction = champDataService.getTransaction(tId);
320
321       if (tId != null && transaction == null) {
322         throw new ChampServiceException("transactionId not found", Status.BAD_REQUEST);
323       }
324       retrieved = champDataService.getRelationship(rId, Optional.ofNullable(transaction));
325       if (retrieved == null) {
326         response = Response.status(Status.NOT_FOUND).entity(rId + " not found").build();
327         return response;
328       }
329       response = Response.status(Status.OK).entity(mapper.writeValueAsString(retrieved)).build();
330
331     } catch (IOException e) {
332       response = Response.status(Status.BAD_REQUEST).entity("Unable to parse the payload").build();
333     } catch (ChampServiceException ce) {
334       response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
335     } catch (Exception e) {
336       response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
337       LoggingUtil.logInternalError(logger, e);
338     } finally {
339       LoggingUtil.logRestRequest(logger, auditLogger, req, response);
340       metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "GET", Long.toString(System.currentTimeMillis() - startTimeInMs));
341     }
342     return response;
343   }
344
345   @POST
346   @Path("relationships")
347   @Consumes(MediaType.APPLICATION_JSON)
348   @Produces(MediaType.APPLICATION_JSON)
349   public Response postRelationships(String relationship, @QueryParam("transactionId") String tId,
350       @Context HttpHeaders headers, @Context UriInfo uriInfo, @Context HttpServletRequest req) {
351     LoggingUtil.initMdcContext(req, headers);
352     long startTimeInMs = System.currentTimeMillis();
353     logger.info(ChampMsgs.INCOMING_REQUEST, tId, relationship);
354     Response response = null;
355     try {
356       ChampTransaction transaction = champDataService.getTransaction(tId);
357       if (tId != null && transaction == null) {
358         throw new ChampServiceException("transactionId not found", Status.BAD_REQUEST);
359       }
360       ChampRelationship r = mapper.readValue(relationship, ChampRelationship.class);
361
362       ChampRelationship created = champDataService.storeRelationship(r, Optional.ofNullable(transaction));
363
364       response = Response.status(Status.CREATED).entity(mapper.writeValueAsString(created)).build();
365     } catch (IOException e) {
366       response = Response.status(Status.BAD_REQUEST).entity("Unable to parse the payload").build();
367     } catch (ChampServiceException ce) {
368       response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
369     } catch (Exception e) {
370       response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
371       LoggingUtil.logInternalError(logger, e);
372     } finally {
373       LoggingUtil.logRestRequest(logger, auditLogger, req, response);
374       metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "POST",
375           Long.toString(System.currentTimeMillis() - startTimeInMs));
376     }
377     return response;
378   }
379
380   @PUT
381   @Path("relationships/{rId}")
382   @Consumes(MediaType.APPLICATION_JSON)
383   @Produces(MediaType.APPLICATION_JSON)
384   public Response updateRelationship(@PathParam("rId") String rId, String relationship,
385       @QueryParam("transactionId") String tId, @Context HttpHeaders headers, @Context UriInfo uriInfo,
386       @Context HttpServletRequest req) {
387     LoggingUtil.initMdcContext(req, headers);
388     long startTimeInMs = System.currentTimeMillis();
389     logger.info(ChampMsgs.INCOMING_REQUEST, tId, relationship);
390
391     Response response = null;
392     try {
393       ChampTransaction transaction = champDataService.getTransaction(tId);
394       if (tId != null && transaction == null) {
395         throw new ChampServiceException("transactionId not found", Status.BAD_REQUEST);
396       }
397       ChampRelationship r = mapper.readValue(relationship, ChampRelationship.class);
398       ChampRelationship updated = champDataService.updateRelationship(r, rId, Optional.ofNullable(transaction));
399
400       response = Response.status(Status.OK).entity(mapper.writeValueAsString(updated)).build();
401     } catch (IOException e) {
402       response = Response.status(Status.BAD_REQUEST).entity("Unable to parse the payload").build();
403     } catch (ChampServiceException ce) {
404       response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
405     } catch (Exception e) {
406       response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
407       LoggingUtil.logInternalError(logger, e);
408     } finally {
409       LoggingUtil.logRestRequest(logger, auditLogger, req, response);
410       metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "PUT", Long.toString(System.currentTimeMillis() - startTimeInMs));
411     }
412     return response;
413   }
414
415   @DELETE
416   @Path("relationships/{relationshipId}")
417   public Response deleteRelationship(@PathParam("relationshipId") String relationshipId,
418       @QueryParam("transactionId") String tId, @Context HttpHeaders headers, @Context UriInfo uriInfo,
419       @Context HttpServletRequest req) {
420     LoggingUtil.initMdcContext(req, headers);
421     long startTimeInMs = System.currentTimeMillis();
422     logger.info(ChampMsgs.INCOMING_REQUEST, tId, relationshipId);
423
424     Response response = null;
425     try {
426       ChampTransaction transaction = champDataService.getTransaction(tId);
427       if (tId != null && transaction == null) {
428         throw new ChampServiceException("transactionId not found", Status.BAD_REQUEST);
429       }
430       champDataService.deleteRelationship(relationshipId, Optional.ofNullable(transaction));
431       response = Response.status(Status.OK).build();
432
433     } catch (ChampRelationshipNotExistsException e) {
434       response = Response.status(Status.NOT_FOUND).entity(relationshipId + " not found").build();
435     } catch (ChampServiceException ce) {
436       response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
437     } catch (ChampTransactionException | ChampUnmarshallingException e) {
438       response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
439     } finally {
440       LoggingUtil.logRestRequest(logger, auditLogger, req, response);
441       metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "DELETE",
442           Long.toString(System.currentTimeMillis() - startTimeInMs));
443     }
444     return response;
445   }
446
447   @GET
448   @Path("relationships/filter/")
449   @Produces(MediaType.APPLICATION_JSON)
450   public Response filterMethod(@Context HttpHeaders headers, @Context UriInfo uriInfo,
451       @Context HttpServletRequest req) {
452     LoggingUtil.initMdcContext(req, headers);
453     long startTimeInMs = System.currentTimeMillis();
454     List<ChampRelationship> list;
455     Map<String, Object> filter = new HashMap<>();
456     for (Map.Entry<String, List<String>> e : uriInfo.getQueryParameters().entrySet()) {
457       filter.put(e.getKey(), e.getValue().get(0));
458     }
459     Response response = null;
460     try {
461       list = champDataService.queryRelationships(filter);
462       response = Response.status(Status.OK).type(MediaType.APPLICATION_JSON).entity(mapper.writeValueAsString(list))
463           .build();
464     } catch (JsonProcessingException e) {
465       e.printStackTrace();
466       response = Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
467     } catch (ChampServiceException e1) {
468       response = Response.status(e1.getHttpStatus()).entity(e1.getMessage()).build();
469     } catch (Exception e) {
470       response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
471       LoggingUtil.logInternalError(logger, e);
472     } finally {
473       LoggingUtil.logRestRequest(logger, auditLogger, req, response);
474       metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "GET", Long.toString(System.currentTimeMillis() - startTimeInMs));
475     }
476     return response;
477   }
478
479   @POST
480   @Path("transaction")
481   @Produces(MediaType.TEXT_PLAIN)
482   public Response openTransaction(@Context HttpHeaders headers, @Context UriInfo uriInfo,
483       @Context HttpServletRequest req) {
484     LoggingUtil.initMdcContext(req, headers);
485     long startTimeInMs = System.currentTimeMillis();
486     Status s;
487     String transaction = champDataService.openTransaction();
488
489     s = Status.OK;
490     Response response = Response.status(s).entity(transaction).build();
491     logger.info(ChampMsgs.PROCESS_EVENT, "Opened Transaction with ID: " + transaction, s.toString());
492     LoggingUtil.logRestRequest(logger, auditLogger, req, response);
493     metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "POST", Long.toString(System.currentTimeMillis() - startTimeInMs));
494     return response;
495   }
496
497   @GET
498   @Path("transaction/{tId}")
499   public Response getSpecificTransaction(@PathParam("tId") String tId, @Context HttpHeaders headers,
500       @Context UriInfo uriInfo, @Context HttpServletRequest req) {
501     LoggingUtil.initMdcContext(req, headers);
502     long startTimeInMs = System.currentTimeMillis();
503
504     Response response = null;
505     ChampTransaction transaction = champDataService.getTransaction(tId);
506     if (transaction == null) {
507       response = Response.status(Status.NOT_FOUND).entity("transaction " + tId + " not found").build();
508       return response;
509     }
510
511     try {
512       response = Response.status(Status.OK).entity(mapper.writeValueAsString(tId + " is OPEN")).build();
513     } catch (JsonProcessingException e) {
514       response = Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
515     } catch (Exception e) {
516       response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
517       LoggingUtil.logInternalError(logger, e);
518     } finally {
519       LoggingUtil.logRestRequest(logger, auditLogger, req, response);
520       metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "GET", Long.toString(System.currentTimeMillis() - startTimeInMs));
521     }
522     return response;
523   }
524
525   @PUT
526   @Path("transaction/{tId}")
527   @Produces(MediaType.TEXT_PLAIN)
528   @Consumes(MediaType.APPLICATION_JSON)
529   public Response updateTransaction(String t, @PathParam("tId") String tId, @Context HttpHeaders headers,
530       @Context UriInfo uriInfo, @Context HttpServletRequest req) {
531     LoggingUtil.initMdcContext(req, headers);
532     long startTimeInMs = System.currentTimeMillis();
533     logger.info(ChampMsgs.INCOMING_REQUEST, tId, "COMMIT/ROLLBACK");
534
535     Response response = null;
536     try {
537       JSONObject jsonObj = new JSONObject(t);
538       String method = jsonObj.getString(this.TRANSACTION_METHOD);
539
540       if (method.equals("commit")) {
541         champDataService.commitTransaction(tId);
542         response = Response.status(Status.OK).entity("COMMITTED").build();
543
544       } else if (method.equals("rollback")) {
545         champDataService.rollbackTransaction(tId);
546         response = Response.status(Status.OK).entity("ROLLED BACK").build();
547       } else {
548         response = Response.status(Status.BAD_REQUEST).entity("Invalid Method: " + method).build();
549         return response;
550       }
551
552     } catch (ChampTransactionException e) {
553       response = Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
554     } catch (JSONException e) {
555       response = Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
556     } catch (ChampServiceException e) {
557       response = Response.status(e.getHttpStatus()).entity(e.getMessage()).build();
558     } catch (Exception e) {
559       response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
560       LoggingUtil.logInternalError(logger, e);
561     } finally {
562       LoggingUtil.logRestRequest(logger, auditLogger, req, response);
563       metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "PUT", Long.toString(System.currentTimeMillis() - startTimeInMs));
564     }
565     return response;
566   }
567
568 }