2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 Amdocs
7 * ===================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.champ;
24 import java.io.IOException;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.List;
29 import java.util.Optional;
30 import java.util.Timer;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.ws.rs.Consumes;
34 import javax.ws.rs.DELETE;
35 import javax.ws.rs.GET;
36 import javax.ws.rs.POST;
37 import javax.ws.rs.PUT;
38 import javax.ws.rs.Path;
39 import javax.ws.rs.PathParam;
40 import javax.ws.rs.Produces;
41 import javax.ws.rs.QueryParam;
42 import javax.ws.rs.core.Context;
43 import javax.ws.rs.core.HttpHeaders;
44 import javax.ws.rs.core.MediaType;
45 import javax.ws.rs.core.Response;
46 import javax.ws.rs.core.Response.Status;
47 import javax.ws.rs.core.UriInfo;
49 import org.json.JSONException;
50 import org.json.JSONObject;
51 import org.onap.aai.champcore.ChampTransaction;
52 import org.onap.aai.champcore.exceptions.ChampObjectNotExistsException;
53 import org.onap.aai.champcore.exceptions.ChampRelationshipNotExistsException;
54 import org.onap.aai.champcore.exceptions.ChampTransactionException;
55 import org.onap.aai.champcore.exceptions.ChampUnmarshallingException;
56 import org.onap.aai.champcore.model.ChampObject;
57 import org.onap.aai.champcore.model.ChampRelationship;
58 import org.onap.aai.cl.api.Logger;
59 import org.onap.aai.cl.eelf.LoggerFactory;
60 import org.onap.champ.async.ChampAsyncRequestProcessor;
61 import org.onap.champ.entity.ChampObjectDeserializer;
62 import org.onap.champ.entity.ChampObjectSerializer;
63 import org.onap.champ.entity.ChampRelationshipDeserializer;
64 import org.onap.champ.entity.ChampRelationshipSerializer;
65 import org.onap.champ.exception.ChampServiceException;
66 import org.onap.champ.service.ChampDataService;
67 import org.onap.champ.service.logging.ChampMsgs;
68 import org.onap.champ.service.logging.LoggingUtil;
69 import org.onap.champ.util.ChampProperties;
70 import org.onap.champ.util.ChampServiceConstants;
72 import com.fasterxml.jackson.core.JsonProcessingException;
73 import com.fasterxml.jackson.databind.ObjectMapper;
74 import com.fasterxml.jackson.databind.module.SimpleModule;
77 public class ChampRESTAPI {
79 private ObjectMapper mapper;
81 private ChampDataService champDataService;
82 private String TRANSACTION_METHOD = "method";
85 private Logger logger = LoggerFactory.getInstance().getLogger(ChampRESTAPI.class);
86 Logger auditLogger = LoggerFactory.getInstance().getAuditLogger(ChampRESTAPI.class.getName());
87 private static Logger metricsLogger = LoggerFactory.getInstance().getMetricsLogger(ChampRESTAPI.class.getName());
89 public ChampRESTAPI(ChampDataService champDataService, ChampAsyncRequestProcessor champAsyncRequestProcessor) {
90 this.champDataService = champDataService;
92 // Async request handling is optional.
93 if (champAsyncRequestProcessor != null) {
94 timer = new Timer("ChampAsyncRequestProcessor-1");
95 timer.schedule(champAsyncRequestProcessor, champAsyncRequestProcessor.getRequestPollingTimeSeconds(),
96 champAsyncRequestProcessor.getRequestPollingTimeSeconds());
99 mapper = new ObjectMapper();
100 SimpleModule module = new SimpleModule();
101 module.addSerializer(ChampObject.class, new ChampObjectSerializer());
102 module.addDeserializer(ChampObject.class, new ChampObjectDeserializer());
103 module.addSerializer(ChampRelationship.class, new ChampRelationshipSerializer());
104 module.addDeserializer(ChampRelationship.class, new ChampRelationshipDeserializer());
105 mapper.registerModule(module);
110 @Produces(MediaType.TEXT_PLAIN)
111 public Response echo() {
112 return Response.ok().entity("alive").build();
116 @Path("objects/{objectId}")
117 @Produces(MediaType.APPLICATION_JSON)
118 public Response getObject(@PathParam("objectId") String objectId, @QueryParam("transactionId") String tId,
119 @Context HttpHeaders headers, @Context UriInfo uriInfo, @Context HttpServletRequest req) {
120 LoggingUtil.initMdcContext(req, headers);
121 long startTimeInMs = System.currentTimeMillis();
122 logger.info(ChampMsgs.INCOMING_REQUEST, tId, objectId);
124 Response response = null;
125 ChampObject retrieved;
128 ChampTransaction transaction = champDataService.getTransaction(tId);
130 if (tId != null && transaction == null) {
131 throw new ChampServiceException("transactionId not found", Status.BAD_REQUEST);
133 retrieved = champDataService.getObject(objectId, Optional.ofNullable(transaction));
134 if (retrieved == null) {
135 response = Response.status(Status.NOT_FOUND).entity(objectId + " not found").build();
137 response = Response.status(Status.OK).entity(mapper.writeValueAsString(retrieved)).build();
140 } catch (JsonProcessingException e) {
141 response = Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
142 } catch (ChampServiceException ce) {
143 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
145 logger.debug(response.getEntity().toString());
146 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
147 metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "GET", Long.toString(System.currentTimeMillis() - startTimeInMs));
154 @Path("objects/{objectId}")
155 public Response deleteObject(@PathParam("objectId") String objectId, @QueryParam("transactionId") String tId,
156 @Context HttpHeaders headers, @Context UriInfo uriInfo, @Context HttpServletRequest req) {
157 LoggingUtil.initMdcContext(req, headers);
158 long startTimeInMs = System.currentTimeMillis();
159 logger.info(ChampMsgs.INCOMING_REQUEST, tId, objectId);
160 ChampObject retrieved;
161 Response response = null;
163 ChampTransaction transaction = champDataService.getTransaction(tId);
165 if (tId != null && transaction == null) {
166 throw new ChampServiceException("transactionId not found", Status.BAD_REQUEST);
168 champDataService.deleteObject(objectId, Optional.ofNullable(transaction));
170 response = Response.status(Status.OK).build();
171 } catch (ChampObjectNotExistsException e) {
172 response = Response.status(Status.NOT_FOUND).entity(objectId + " not found").build();
173 } catch (ChampServiceException ce) {
174 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
175 } catch (ChampTransactionException | ChampUnmarshallingException e) {
176 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
178 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
179 metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "DELETE",
180 Long.toString(System.currentTimeMillis() - startTimeInMs));
187 @Consumes(MediaType.APPLICATION_JSON)
188 @Produces(MediaType.APPLICATION_JSON)
189 public Response postObject(String champObj, @QueryParam("transactionId") String tId, @Context HttpHeaders headers,
190 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
191 LoggingUtil.initMdcContext(req, headers);
192 long startTimeInMs = System.currentTimeMillis();
193 logger.info(ChampMsgs.INCOMING_REQUEST, tId, champObj);
194 Response response = null;
196 ChampTransaction transaction = champDataService.getTransaction(tId);
197 if (tId != null && transaction == null) {
198 throw new ChampServiceException("transactionId not found", Status.BAD_REQUEST);
200 ChampObject champObject = mapper.readValue(champObj, ChampObject.class);
202 ChampObject created = champDataService.storeObject(champObject, Optional.ofNullable(transaction));
203 response = Response.status(Status.CREATED).entity(mapper.writeValueAsString(created)).build();
204 } catch (IOException e) {
205 response = Response.status(Status.BAD_REQUEST).entity("Unable to parse the payload").build();
206 } catch (ChampServiceException ce) {
207 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
208 } catch (Exception e) {
209 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
210 LoggingUtil.logInternalError(logger, e);
212 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
213 metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "POST",
214 Long.toString(System.currentTimeMillis() - startTimeInMs));
220 @Path("objects/{objectId}")
221 @Consumes(MediaType.APPLICATION_JSON)
222 @Produces(MediaType.APPLICATION_JSON)
223 public Response putObject(@PathParam("objectId") String objectId, String champObj,
224 @QueryParam("transactionId") String tId, @Context HttpHeaders headers, @Context UriInfo uriInfo,
225 @Context HttpServletRequest req) {
226 LoggingUtil.initMdcContext(req, headers);
227 long startTimeInMs = System.currentTimeMillis();
228 logger.info(ChampMsgs.INCOMING_REQUEST, tId, objectId + " " + champObj);
230 Response response = null;
232 ChampTransaction transaction = champDataService.getTransaction(tId);
233 if (tId != null && transaction == null) {
234 throw new ChampServiceException("transactionId not found", Status.BAD_REQUEST);
237 ChampObject co = mapper.readValue(champObj, ChampObject.class);
238 // check if key is present or if it equals the key that is in the URI
239 ChampObject updated = champDataService.replaceObject(co, objectId, Optional.ofNullable(transaction));
241 response = Response.status(Status.OK).entity(mapper.writeValueAsString(updated)).build();
242 } catch (IOException e) {
243 response = Response.status(Status.BAD_REQUEST).entity("Unable to parse the payload").build();
244 } catch (ChampServiceException ce) {
245 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
246 } catch (Exception e) {
247 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
248 LoggingUtil.logInternalError(logger, e);
250 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
251 metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "PUT", Long.toString(System.currentTimeMillis() - startTimeInMs));
257 @Path("objects/relationships/{oId}")
258 @Produces(MediaType.APPLICATION_JSON)
259 public Response getEdges(@PathParam("oId") String oId, @QueryParam("transactionId") String tId,
260 @Context HttpHeaders headers, @Context UriInfo uriInfo, @Context HttpServletRequest req) {
261 LoggingUtil.initMdcContext(req, headers);
262 long startTimeInMs = System.currentTimeMillis();
263 List<ChampRelationship> retrieved;
264 Optional<ChampObject> rObject;
265 Response response = null;
266 ChampTransaction transaction = null;
269 retrieved = champDataService.getRelationshipsByObject(oId, Optional.ofNullable(transaction));
270 response = Response.status(Status.OK).entity(mapper.writeValueAsString(retrieved)).build();
271 } catch (JsonProcessingException e) {
272 response = Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
273 } catch (ChampServiceException ce) {
274 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
275 } catch (Exception e) {
276 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
277 LoggingUtil.logInternalError(logger, e);
279 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
280 metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "GET", Long.toString(System.currentTimeMillis() - startTimeInMs));
286 @Path("objects/filter/")
287 @Produces(MediaType.APPLICATION_JSON)
288 public Response filterObject(@Context HttpHeaders headers, @Context UriInfo uriInfo,
289 @Context HttpServletRequest req) {
290 LoggingUtil.initMdcContext(req, headers);
291 long startTimeInMs = System.currentTimeMillis();
292 String propertiesKey = ChampProperties.get(ChampServiceConstants.CHAMP_COLLECTION_PROPERTIES_KEY);
293 List<ChampObject> objects;
294 Map<String, Object> filter = new HashMap<>();
296 for (Map.Entry<String, List<String>> e : uriInfo.getQueryParameters().entrySet()) {
297 if (!e.getKey().equals(propertiesKey)) {
298 filter.put(e.getKey(), e.getValue().get(0));
302 HashSet<String> properties;
303 if (uriInfo.getQueryParameters().containsKey(propertiesKey)) {
304 properties = new HashSet<>(uriInfo.getQueryParameters().get(propertiesKey));
306 properties = new HashSet<>();
309 Response response = null;
311 objects = champDataService.queryObjects(filter, properties);
312 response = Response.status(Status.OK).type(MediaType.APPLICATION_JSON).entity(mapper.writeValueAsString(objects))
314 } catch (JsonProcessingException e) {
316 response = Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
317 } catch (ChampServiceException e1) {
318 response = Response.status(e1.getHttpStatus()).entity(e1.getMessage()).build();
319 } catch (Exception e) {
320 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
321 LoggingUtil.logInternalError(logger, e);
323 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
324 metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "GET", Long.toString(System.currentTimeMillis() - startTimeInMs));
330 @Path("relationships/{rId}")
331 @Produces(MediaType.APPLICATION_JSON)
332 public Response getRelationship(@PathParam("rId") String rId, @QueryParam("transactionId") String tId,
333 @Context HttpHeaders headers, @Context UriInfo uriInfo, @Context HttpServletRequest req) {
334 LoggingUtil.initMdcContext(req, headers);
335 long startTimeInMs = System.currentTimeMillis();
336 logger.info(ChampMsgs.INCOMING_REQUEST, tId, rId);
337 ChampRelationship retrieved;
338 Response response = null;
340 ChampTransaction transaction = champDataService.getTransaction(tId);
342 if (tId != null && transaction == null) {
343 throw new ChampServiceException("transactionId not found", Status.BAD_REQUEST);
345 retrieved = champDataService.getRelationship(rId, Optional.ofNullable(transaction));
346 if (retrieved == null) {
347 response = Response.status(Status.NOT_FOUND).entity(rId + " not found").build();
350 response = Response.status(Status.OK).entity(mapper.writeValueAsString(retrieved)).build();
352 } catch (IOException e) {
353 response = Response.status(Status.BAD_REQUEST).entity("Unable to parse the payload").build();
354 } catch (ChampServiceException ce) {
355 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
356 } catch (Exception e) {
357 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
358 LoggingUtil.logInternalError(logger, e);
360 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
361 metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "GET", Long.toString(System.currentTimeMillis() - startTimeInMs));
367 @Path("relationships")
368 @Consumes(MediaType.APPLICATION_JSON)
369 @Produces(MediaType.APPLICATION_JSON)
370 public Response postRelationships(String relationship, @QueryParam("transactionId") String tId,
371 @Context HttpHeaders headers, @Context UriInfo uriInfo, @Context HttpServletRequest req) {
372 LoggingUtil.initMdcContext(req, headers);
373 long startTimeInMs = System.currentTimeMillis();
374 logger.info(ChampMsgs.INCOMING_REQUEST, tId, relationship);
375 Response response = null;
377 ChampTransaction transaction = champDataService.getTransaction(tId);
378 if (tId != null && transaction == null) {
379 throw new ChampServiceException("transactionId not found", Status.BAD_REQUEST);
381 ChampRelationship r = mapper.readValue(relationship, ChampRelationship.class);
383 ChampRelationship created = champDataService.storeRelationship(r, Optional.ofNullable(transaction));
385 response = Response.status(Status.CREATED).entity(mapper.writeValueAsString(created)).build();
386 } catch (IOException e) {
387 response = Response.status(Status.BAD_REQUEST).entity("Unable to parse the payload").build();
388 } catch (ChampServiceException ce) {
389 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
390 } catch (Exception e) {
391 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
392 LoggingUtil.logInternalError(logger, e);
394 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
395 metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "POST",
396 Long.toString(System.currentTimeMillis() - startTimeInMs));
402 @Path("relationships/{rId}")
403 @Consumes(MediaType.APPLICATION_JSON)
404 @Produces(MediaType.APPLICATION_JSON)
405 public Response updateRelationship(@PathParam("rId") String rId, String relationship,
406 @QueryParam("transactionId") String tId, @Context HttpHeaders headers, @Context UriInfo uriInfo,
407 @Context HttpServletRequest req) {
408 LoggingUtil.initMdcContext(req, headers);
409 long startTimeInMs = System.currentTimeMillis();
410 logger.info(ChampMsgs.INCOMING_REQUEST, tId, relationship);
412 Response response = null;
414 ChampTransaction transaction = champDataService.getTransaction(tId);
415 if (tId != null && transaction == null) {
416 throw new ChampServiceException("transactionId not found", Status.BAD_REQUEST);
418 ChampRelationship r = mapper.readValue(relationship, ChampRelationship.class);
419 ChampRelationship updated = champDataService.updateRelationship(r, rId, Optional.ofNullable(transaction));
421 response = Response.status(Status.OK).entity(mapper.writeValueAsString(updated)).build();
422 } catch (IOException e) {
423 response = Response.status(Status.BAD_REQUEST).entity("Unable to parse the payload").build();
424 } catch (ChampServiceException ce) {
425 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
426 } catch (Exception e) {
427 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
428 LoggingUtil.logInternalError(logger, e);
430 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
431 metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "PUT", Long.toString(System.currentTimeMillis() - startTimeInMs));
437 @Path("relationships/{relationshipId}")
438 public Response deleteRelationship(@PathParam("relationshipId") String relationshipId,
439 @QueryParam("transactionId") String tId, @Context HttpHeaders headers, @Context UriInfo uriInfo,
440 @Context HttpServletRequest req) {
441 LoggingUtil.initMdcContext(req, headers);
442 long startTimeInMs = System.currentTimeMillis();
443 logger.info(ChampMsgs.INCOMING_REQUEST, tId, relationshipId);
445 Response response = null;
447 ChampTransaction transaction = champDataService.getTransaction(tId);
448 if (tId != null && transaction == null) {
449 throw new ChampServiceException("transactionId not found", Status.BAD_REQUEST);
451 champDataService.deleteRelationship(relationshipId, Optional.ofNullable(transaction));
452 response = Response.status(Status.OK).build();
454 } catch (ChampRelationshipNotExistsException e) {
455 response = Response.status(Status.NOT_FOUND).entity(relationshipId + " not found").build();
456 } catch (ChampServiceException ce) {
457 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
458 } catch (ChampTransactionException | ChampUnmarshallingException e) {
459 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
461 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
462 metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "DELETE",
463 Long.toString(System.currentTimeMillis() - startTimeInMs));
469 @Path("relationships/filter/")
470 @Produces(MediaType.APPLICATION_JSON)
471 public Response filterMethod(@Context HttpHeaders headers, @Context UriInfo uriInfo,
472 @Context HttpServletRequest req) {
473 LoggingUtil.initMdcContext(req, headers);
474 long startTimeInMs = System.currentTimeMillis();
475 List<ChampRelationship> list;
476 Map<String, Object> filter = new HashMap<>();
477 for (Map.Entry<String, List<String>> e : uriInfo.getQueryParameters().entrySet()) {
478 filter.put(e.getKey(), e.getValue().get(0));
480 Response response = null;
482 list = champDataService.queryRelationships(filter);
483 response = Response.status(Status.OK).type(MediaType.APPLICATION_JSON).entity(mapper.writeValueAsString(list))
485 } catch (JsonProcessingException e) {
487 response = Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
488 } catch (ChampServiceException e1) {
489 response = Response.status(e1.getHttpStatus()).entity(e1.getMessage()).build();
490 } catch (Exception e) {
491 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
492 LoggingUtil.logInternalError(logger, e);
494 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
495 metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "GET", Long.toString(System.currentTimeMillis() - startTimeInMs));
502 @Produces(MediaType.TEXT_PLAIN)
503 public Response openTransaction(@Context HttpHeaders headers, @Context UriInfo uriInfo,
504 @Context HttpServletRequest req) {
505 LoggingUtil.initMdcContext(req, headers);
506 long startTimeInMs = System.currentTimeMillis();
508 String transaction = champDataService.openTransaction();
511 Response response = Response.status(s).entity(transaction).build();
512 logger.info(ChampMsgs.PROCESS_EVENT, "Opened Transaction with ID: " + transaction, s.toString());
513 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
514 metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "POST", Long.toString(System.currentTimeMillis() - startTimeInMs));
519 @Path("transaction/{tId}")
520 public Response getSpecificTransaction(@PathParam("tId") String tId, @Context HttpHeaders headers,
521 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
522 LoggingUtil.initMdcContext(req, headers);
523 long startTimeInMs = System.currentTimeMillis();
525 Response response = null;
526 ChampTransaction transaction = champDataService.getTransaction(tId);
527 if (transaction == null) {
528 response = Response.status(Status.NOT_FOUND).entity("transaction " + tId + " not found").build();
533 response = Response.status(Status.OK).entity(mapper.writeValueAsString(tId + " is OPEN")).build();
534 } catch (JsonProcessingException e) {
535 response = Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
536 } catch (Exception e) {
537 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
538 LoggingUtil.logInternalError(logger, e);
540 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
541 metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "GET", Long.toString(System.currentTimeMillis() - startTimeInMs));
547 @Path("transaction/{tId}")
548 @Produces(MediaType.TEXT_PLAIN)
549 @Consumes(MediaType.APPLICATION_JSON)
550 public Response updateTransaction(String t, @PathParam("tId") String tId, @Context HttpHeaders headers,
551 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
552 LoggingUtil.initMdcContext(req, headers);
553 long startTimeInMs = System.currentTimeMillis();
554 logger.info(ChampMsgs.INCOMING_REQUEST, tId, "COMMIT/ROLLBACK");
556 Response response = null;
558 JSONObject jsonObj = new JSONObject(t);
559 String method = jsonObj.getString(this.TRANSACTION_METHOD);
561 if (method.equals("commit")) {
562 champDataService.commitTransaction(tId);
563 response = Response.status(Status.OK).entity("COMMITTED").build();
565 } else if (method.equals("rollback")) {
566 champDataService.rollbackTransaction(tId);
567 response = Response.status(Status.OK).entity("ROLLED BACK").build();
569 response = Response.status(Status.BAD_REQUEST).entity("Invalid Method: " + method).build();
573 } catch (ChampTransactionException e) {
574 response = Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
575 } catch (JSONException e) {
576 response = Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
577 } catch (ChampServiceException e) {
578 response = Response.status(e.getHttpStatus()).entity(e.getMessage()).build();
579 } catch (Exception e) {
580 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
581 LoggingUtil.logInternalError(logger, e);
583 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
584 metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "PUT", Long.toString(System.currentTimeMillis() - startTimeInMs));