support for v20
[aai/resources.git] / aai-resources / src / main / java / org / onap / aai / rest / LegacyMoxyConsumer.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aai.rest;
21
22 import io.swagger.jaxrs.PATCH;
23 import org.javatuples.Pair;
24 import org.onap.aai.concurrent.AaiCallable;
25 import org.onap.aai.config.SpringContextAware;
26 import org.onap.aai.exceptions.AAIException;
27 import org.onap.aai.introspection.Introspector;
28 import org.onap.aai.introspection.Loader;
29 import org.onap.aai.parsers.query.QueryParser;
30 import org.onap.aai.rest.db.DBRequest;
31 import org.onap.aai.rest.db.HttpEntry;
32 import org.onap.aai.rest.exceptions.AAIInvalidXMLNamespace;
33 import org.onap.aai.rest.util.ValidateEncoding;
34 import org.onap.aai.restcore.HttpMethod;
35 import org.onap.aai.restcore.RESTAPI;
36 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
37 import org.onap.aai.setup.SchemaVersion;
38 import org.onap.aai.util.AAIConstants;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.stereotype.Controller;
42
43 import javax.servlet.http.HttpServletRequest;
44 import javax.ws.rs.*;
45 import javax.ws.rs.core.*;
46 import java.io.UnsupportedEncodingException;
47 import java.net.URI;
48 import java.util.*;
49 import java.util.stream.Collectors;
50
51 /**
52  * The Class LegacyMoxyConsumer.
53  */
54 @Controller
55 @Path("{version: v[1-9][0-9]*|latest}")
56 public class LegacyMoxyConsumer extends RESTAPI {
57
58         private static final Logger logger = LoggerFactory.getLogger(LegacyMoxyConsumer.class.getName());
59
60         /**
61          *
62          * @param content
63          * @param versionParam
64          * @param uri
65          * @param headers
66          * @param info
67          * @param req
68          * @return
69          */
70         @PUT
71         @Path("/{uri: .+}")
72         @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
73         @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
74         public Response update (String content, @PathParam("version")String versionParam, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo info, @Context HttpServletRequest req) {
75                 MediaType mediaType = headers.getMediaType();
76                 return this.handleWrites(mediaType, HttpMethod.PUT, content, versionParam, uri, headers, info);
77         }
78
79         /**
80          * Update relationship.
81          *
82          * @param content the content
83          * @param versionParam the version param
84          * @param uri the uri
85          * @param headers the headers
86          * @param info the info
87          * @param req the req
88          * @return the response
89          */
90         @PUT
91         @Path("/{uri: .+}/relationship-list/relationship")
92         @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
93         @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
94         public Response updateRelationship (String content, @PathParam("version")String versionParam, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo info, @Context HttpServletRequest req) {
95
96                 String sourceOfTruth = headers.getRequestHeaders().getFirst("X-FromAppId");
97                 String transId = headers.getRequestHeaders().getFirst("X-TransactionId");
98                 MediaType inputMediaType = headers.getMediaType();
99                 Response response;
100                 Loader loader;
101                 TransactionalGraphEngine dbEngine = null;
102                 boolean success = true;
103
104                 try {
105                         validateRequest(info);
106                         SchemaVersion version = new SchemaVersion(versionParam);
107
108                         HttpEntry traversalUriHttpEntry = SpringContextAware.getBean("traversalUriHttpEntry", HttpEntry.class);
109                         traversalUriHttpEntry.setHttpEntryProperties(version);
110                         loader = traversalUriHttpEntry.getLoader();
111                         dbEngine = traversalUriHttpEntry.getDbEngine();
112
113                         URI uriObject = UriBuilder.fromPath(uri).build();
114                         this.validateURI(uriObject);
115
116                         QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject);
117
118                         Introspector wrappedEntity = loader.unmarshal("relationship", content, org.onap.aai.restcore.MediaType.getEnum(this.getInputMediaType(inputMediaType)));
119
120                         DBRequest request = new DBRequest.Builder(HttpMethod.PUT_EDGE, uriObject, uriQuery, wrappedEntity, headers, info, transId).build();
121                         List<DBRequest> requests = new ArrayList<>();
122                         requests.add(request);
123                         Pair<Boolean, List<Pair<URI, Response>>> responsesTuple  = traversalUriHttpEntry.process(requests, sourceOfTruth);
124
125                         response = responsesTuple.getValue1().get(0).getValue1();
126                         success = responsesTuple.getValue0();
127
128                 } catch (AAIException e) {
129                         response = consumerExceptionResponseGenerator(headers, info, HttpMethod.PUT, e);
130                         success = false;
131                 } catch (Exception e) {
132                         AAIException aaiException = new AAIException("AAI_4000", e);
133                         response = consumerExceptionResponseGenerator(headers, info, HttpMethod.PUT, aaiException);
134                         success = false;
135                 } finally {
136                         if (dbEngine != null) {
137                                 if (success) {
138                                         dbEngine.commit();
139                                 } else {
140                                         dbEngine.rollback();
141                                 }
142                         }
143
144                 }
145
146                 return response;
147         }
148
149         /**
150          * Patch.
151          *
152          * @param content the content
153          * @param versionParam the version param
154          * @param uri the uri
155          * @param headers the headers
156          * @param info the info
157          * @param req the req
158          * @return the response
159          */
160         @PATCH
161         @Path("/{uri: .+}")
162         @Consumes({ "application/merge-patch+json" })
163         @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
164         public Response patch (String content, @PathParam("version")String versionParam, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo info, @Context HttpServletRequest req) {
165
166                 MediaType mediaType = MediaType.APPLICATION_JSON_TYPE;
167                 return this.handleWrites(mediaType, HttpMethod.MERGE_PATCH, content, versionParam, uri, headers, info);
168
169         }
170
171         /**
172          * Gets the legacy.
173          *
174          * @param content the content
175          * @param versionParam the version param
176          * @param uri the uri
177          * @param depthParam the depth param
178          * @param cleanUp the clean up
179          * @param headers the headers
180          * @param info the info
181          * @param req the req
182          * @return the legacy
183          */
184         @GET
185         @Path("/{uri: .+}")
186         @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
187         @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
188         public Response getLegacy (String content, @DefaultValue("-1") @QueryParam("resultIndex") String resultIndex, @DefaultValue("-1") @QueryParam("resultSize") String resultSize, @PathParam("version")String versionParam, @PathParam("uri") @Encoded String uri, @DefaultValue("all") @QueryParam("depth") String depthParam, @DefaultValue("false") @QueryParam("cleanup") String cleanUp, @Context HttpHeaders headers, @Context UriInfo info, @Context HttpServletRequest req) {
189                 return runner(AAIConstants.AAI_CRUD_TIMEOUT_ENABLED,
190                                 AAIConstants.AAI_CRUD_TIMEOUT_APP,
191                                 AAIConstants.AAI_CRUD_TIMEOUT_LIMIT,
192                                 headers,
193                                 info,
194                                 HttpMethod.GET,
195                                 new AaiCallable<Response>() {
196                                         @Override
197                                         public Response process() {
198                                                 return getLegacy(content, versionParam, uri, depthParam, cleanUp, headers, info, req, new HashSet<String>(), resultIndex, resultSize);
199                                         }
200                                 }
201                 );
202         }
203
204         /**
205          * This method exists as a workaround for filtering out undesired query params while routing between REST consumers
206          *
207          * @param content
208          * @param versionParam
209          * @param uri
210          * @param depthParam
211          * @param cleanUp
212          * @param headers
213          * @param info
214          * @param req
215          * @param removeQueryParams
216          * @return
217          */
218         public Response getLegacy(String content, String versionParam, String uri, String depthParam, String cleanUp,  HttpHeaders headers, UriInfo info, HttpServletRequest req, Set<String> removeQueryParams, String resultIndex, String resultSize) {
219                 String sourceOfTruth = headers.getRequestHeaders().getFirst("X-FromAppId");
220                 String transId = headers.getRequestHeaders().getFirst("X-TransactionId");
221                 Response response;
222                 TransactionalGraphEngine dbEngine = null;
223                 Loader loader;
224
225                 try {
226                         validateRequest(info);
227                         SchemaVersion version = new SchemaVersion(versionParam);
228
229                         final HttpEntry traversalUriHttpEntry = SpringContextAware.getBean("traversalUriHttpEntry", HttpEntry.class);
230                         String serverBase = req.getRequestURL().toString().replaceAll("/(v[0-9]+|latest)/.*", "/");
231                         traversalUriHttpEntry.setHttpEntryProperties(version, serverBase);
232                         dbEngine = traversalUriHttpEntry.getDbEngine();
233                         loader = traversalUriHttpEntry.getLoader();
234                         MultivaluedMap<String, String> params = info.getQueryParameters();
235
236                         params = removeNonFilterableParams(params);
237
238                         uri = uri.split("\\?")[0];
239
240                         URI uriObject = UriBuilder.fromPath(uri).build();
241
242                         QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject, params);
243
244                         String objType = "";
245                         if (!uriQuery.getContainerType().equals("")) {
246                                 objType = uriQuery.getContainerType();
247                         } else {
248                                 objType = uriQuery.getResultType();
249                         }
250                         Introspector obj = loader.introspectorFromName(objType);
251                         DBRequest request =
252                                         new DBRequest.Builder(HttpMethod.GET, uriObject, uriQuery, obj, headers, info, transId).build();
253                         List<DBRequest> requests = new ArrayList<>();
254                         requests.add(request);
255                         if (resultIndex != null && !"-1".equals(resultIndex) && resultSize != null && !"-1".equals(resultSize)) {
256                                 traversalUriHttpEntry.setPaginationIndex(Integer.parseInt(resultIndex));
257                                 traversalUriHttpEntry.setPaginationBucket(Integer.parseInt(resultSize));
258                         }
259                         Pair<Boolean, List<Pair<URI, Response>>> responsesTuple = traversalUriHttpEntry.process(requests, sourceOfTruth);
260
261                         response = responsesTuple.getValue1().get(0).getValue1();
262
263                 } catch (AAIException e) {
264                         response = consumerExceptionResponseGenerator(headers, info, HttpMethod.GET, e);
265                 } catch (Exception e ) {
266                         AAIException ex = new AAIException("AAI_4000", e);
267
268                         response = consumerExceptionResponseGenerator(headers, info, HttpMethod.GET, ex);
269                 } finally {
270                         if (dbEngine != null) {
271                                 if (cleanUp.equals("true")) {
272                                         dbEngine.commit();
273                                 } else {
274                                         dbEngine.rollback();
275                                 }
276                         }
277                 }
278
279                 return response;
280         }
281
282         private MultivaluedMap<String, String> removeNonFilterableParams(MultivaluedMap<String, String> params) {
283
284                 String[] toRemove = { "depth", "cleanup", "nodes-only", "format", "resultIndex", "resultSize", "skip-related-to"};
285                 Set<String> toRemoveSet = Arrays.stream(toRemove).collect(Collectors.toSet());
286
287                 MultivaluedMap<String, String> cleanedParams = new MultivaluedHashMap<>();
288                 params.keySet().forEach(k -> {
289                         if (!toRemoveSet.contains(k)) {
290                                 cleanedParams.addAll(k, params.get(k));
291                         }
292                 });
293
294                 return cleanedParams;
295         }
296         /**
297          * Delete.
298          *
299          * @param versionParam the version param
300          * @param uri the uri
301          * @param headers the headers
302          * @param info the info
303          * @param resourceVersion the resource version
304          * @param req the req
305          * @return the response
306          */
307         @DELETE
308         @Path("/{uri: .+}")
309         @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
310         public Response delete (@PathParam("version")String versionParam, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo info, @QueryParam("resource-version")String resourceVersion, @Context HttpServletRequest req) {
311
312
313                 String outputMediaType = getMediaType(headers.getAcceptableMediaTypes());
314                 String sourceOfTruth = headers.getRequestHeaders().getFirst("X-FromAppId");
315                 String transId = headers.getRequestHeaders().getFirst("X-TransactionId");
316
317                 TransactionalGraphEngine dbEngine = null;
318                 Response response;
319
320                 boolean success = true;
321
322                 try {
323
324                         validateRequest(info);
325                         SchemaVersion version = new SchemaVersion(versionParam);
326
327                         HttpEntry traversalUriHttpEntry = SpringContextAware.getBean("traversalUriHttpEntry", HttpEntry.class);
328                         traversalUriHttpEntry.setHttpEntryProperties(version);
329                         dbEngine = traversalUriHttpEntry.getDbEngine();
330                         Loader loader = traversalUriHttpEntry.getLoader();
331
332                         URI uriObject = UriBuilder.fromPath(uri).build();
333
334                         QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject);
335                         String objType = uriQuery.getResultType();
336                         Introspector obj = loader.introspectorFromName(objType);
337
338                         DBRequest request = new DBRequest.Builder(HttpMethod.DELETE, uriObject, uriQuery, obj, headers, info, transId).build();
339                         List<DBRequest> requests = new ArrayList<>();
340                         requests.add(request);
341                         Pair<Boolean, List<Pair<URI, Response>>> responsesTuple  = traversalUriHttpEntry.process(requests, sourceOfTruth);
342
343                         response = responsesTuple.getValue1().get(0).getValue1();
344                         success = responsesTuple.getValue0();
345
346                 } catch (AAIException e) {
347                         response = consumerExceptionResponseGenerator(headers, info, HttpMethod.DELETE, e);
348                         success = false;
349                 } catch (Exception e) {
350                         AAIException ex = new AAIException("AAI_4000", e);
351                         response = consumerExceptionResponseGenerator(headers, info, HttpMethod.DELETE, ex);
352                         success = false;
353                 } finally {
354                         if (dbEngine != null) {
355                                 if (success) {
356                                         dbEngine.commit();
357                                 } else {
358                                         dbEngine.rollback();
359                                 }
360                         }
361                 }
362
363                 return response;
364         }
365
366         /**
367          * This whole method does nothing because the body is being dropped while fielding the request.
368          *
369          * @param content the content
370          * @param versionParam the version param
371          * @param uri the uri
372          * @param headers the headers
373          * @param info the info
374          * @param req the req
375          * @return the response
376          */
377         @DELETE
378         @Path("/{uri: .+}/relationship-list/relationship")
379         @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
380         @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
381         public Response deleteRelationship (String content, @PathParam("version")String versionParam, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo info, @Context HttpServletRequest req) {
382
383                 MediaType inputMediaType = headers.getMediaType();
384                 String sourceOfTruth = headers.getRequestHeaders().getFirst("X-FromAppId");
385                 String transId = headers.getRequestHeaders().getFirst("X-TransactionId");
386                 Loader loader;
387                 TransactionalGraphEngine dbEngine = null;
388                 Response response;
389
390                 boolean success = true;
391
392                 try {
393                         this.validateRequest(info);
394                         SchemaVersion version = new SchemaVersion(versionParam);
395
396                         HttpEntry traversalUriHttpEntry = SpringContextAware.getBean("traversalUriHttpEntry", HttpEntry.class);
397                         traversalUriHttpEntry.setHttpEntryProperties(version);
398                         loader = traversalUriHttpEntry.getLoader();
399                         dbEngine = traversalUriHttpEntry.getDbEngine();
400
401                         if (content.equals("")) {
402                                 throw new AAIException("AAI_3102", "You must supply a relationship");
403                         }
404                         URI uriObject = UriBuilder.fromPath(uri).build();
405                         this.validateURI(uriObject);
406
407                         QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject);
408
409                         Introspector wrappedEntity = loader.unmarshal("relationship", content, org.onap.aai.restcore.MediaType.getEnum(this.getInputMediaType(inputMediaType)));
410
411                         DBRequest request = new DBRequest.Builder(HttpMethod.DELETE_EDGE, uriObject, uriQuery, wrappedEntity, headers, info, transId).build();
412                         List<DBRequest> requests = new ArrayList<>();
413                         requests.add(request);
414                         Pair<Boolean, List<Pair<URI, Response>>> responsesTuple  = traversalUriHttpEntry.process(requests, sourceOfTruth);
415
416                         response = responsesTuple.getValue1().get(0).getValue1();
417                         success = responsesTuple.getValue0();
418                 } catch (AAIException e) {
419                         response = consumerExceptionResponseGenerator(headers, info, HttpMethod.DELETE, e);
420                         success = false;
421                 } catch (Exception e) {
422                         AAIException ex = new AAIException("AAI_4000", e);
423                         response = consumerExceptionResponseGenerator(headers, info, HttpMethod.DELETE, ex);
424                         success = false;
425                 } finally {
426                         if (dbEngine != null) {
427                                 if (success) {
428                                         dbEngine.commit();
429                                 } else {
430                                         dbEngine.rollback();
431                                 }
432                         }
433                 }
434
435                 return response;
436         }
437
438         @GET
439         @Path("/{uri: .+}/relationship-list")
440         @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
441         @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
442         public Response getRelationshipList (@DefaultValue("-1") @QueryParam("resultIndex") String resultIndex, @DefaultValue("-1") @QueryParam("resultSize") String resultSize, @PathParam("version")String versionParam, @PathParam("uri") @Encoded String uri, @DefaultValue("false") @QueryParam("cleanup") String cleanUp, @Context HttpHeaders headers, @Context HttpServletRequest req,@Context UriInfo info) {
443                 return runner(AAIConstants.AAI_CRUD_TIMEOUT_ENABLED,
444                                 AAIConstants.AAI_CRUD_TIMEOUT_APP,
445                                 AAIConstants.AAI_CRUD_TIMEOUT_LIMIT,
446                                 headers,
447                                 info,
448                                 HttpMethod.GET,
449                                 new AaiCallable<Response>() {
450                                         @Override
451                                         public Response process() {
452                                                 return getRelationshipList(versionParam, req, uri, cleanUp, headers, info, resultIndex, resultSize);
453                                         }
454                                 }
455                 );
456         }
457
458         /**
459          *
460          * @param versionParam
461          * @param uri
462          * @param cleanUp
463          * @param headers
464          * @param info
465          * @return
466          */
467         public Response getRelationshipList(String versionParam, HttpServletRequest req, String uri, String cleanUp, HttpHeaders headers, UriInfo info, String resultIndex, String resultSize) {
468                 String sourceOfTruth = headers.getRequestHeaders().getFirst("X-FromAppId");
469                 String transId = headers.getRequestHeaders().getFirst("X-TransactionId");
470                 Response response = null;
471                 TransactionalGraphEngine dbEngine = null;
472                 Loader loader = null;
473
474                 try {
475                         validateRequest(info);
476                         SchemaVersion version = new SchemaVersion(versionParam);
477
478                         final HttpEntry traversalUriHttpEntry = SpringContextAware.getBean("traversalUriHttpEntry", HttpEntry.class);
479                         String serverBase = req.getRequestURL().toString().replaceAll("/(v[0-9]+|latest)/.*", "/");
480                         traversalUriHttpEntry.setHttpEntryProperties(version, serverBase);
481                         dbEngine = traversalUriHttpEntry.getDbEngine();
482                         loader = traversalUriHttpEntry.getLoader();
483                         MultivaluedMap<String, String> params = info.getQueryParameters();
484
485                         params = removeNonFilterableParams(params);
486
487                         uri = uri.split("\\?")[0];
488
489                         URI uriObject = UriBuilder.fromPath(uri).build();
490
491                         QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject, params);
492
493                         String objType = "";
494                         if (!uriQuery.getContainerType().equals("")) {
495                                 objType = uriQuery.getContainerType();
496                         } else {
497                                 objType = uriQuery.getResultType();
498                         }
499                         Introspector obj = loader.introspectorFromName(objType);
500                         DBRequest request =
501                                         new DBRequest.Builder(HttpMethod.GET_RELATIONSHIP, uriObject, uriQuery, obj, headers, info, transId).build();
502                         List<DBRequest> requests = new ArrayList<>();
503                         requests.add(request);
504                         if (resultIndex != null && !"-1".equals(resultIndex) && resultSize != null && !"-1".equals(resultSize)) {
505                                 traversalUriHttpEntry.setPaginationIndex(Integer.parseInt(resultIndex));
506                                 traversalUriHttpEntry.setPaginationBucket(Integer.parseInt(resultSize));
507                         }
508                         Pair<Boolean, List<Pair<URI, Response>>> responsesTuple = traversalUriHttpEntry.process(requests, sourceOfTruth);
509
510                         response = responsesTuple.getValue1().get(0).getValue1();
511                 } catch (AAIException e) {
512                         response = consumerExceptionResponseGenerator(headers, info, HttpMethod.GET_RELATIONSHIP, e);
513                 } catch (Exception e ) {
514                         AAIException ex = new AAIException("AAI_4000", e);
515
516                         response = consumerExceptionResponseGenerator(headers, info, HttpMethod.GET_RELATIONSHIP, ex);
517                 } finally {
518                         if (dbEngine != null) {
519                                 if (cleanUp.equals("true")) {
520                                         dbEngine.commit();
521                                 } else {
522                                         dbEngine.rollback();
523                                 }
524                         }
525                 }
526                 return response;
527         }
528
529         /**
530          * Validate request.
531          *
532          * @param info the info
533          * @throws AAIException the AAI exception
534          * @throws UnsupportedEncodingException the unsupported encoding exception
535          */
536         private void validateRequest(UriInfo info) throws AAIException, UnsupportedEncodingException {
537
538                 if (!ValidateEncoding.getInstance().validate(info)) {
539                         throw new AAIException("AAI_3008", "uri=" + getPath(info));
540                 }
541         }
542
543         /**
544          * Gets the path.
545          *
546          * @param info the info
547          * @return the path
548          */
549         private String getPath(UriInfo info) {
550                 String path = info.getPath(false);
551                 MultivaluedMap<String, String> map = info.getQueryParameters(false);
552                 String params = "?";
553                 List<String> parmList = new ArrayList<>();
554                 for (String key : map.keySet()) {
555                         for (String value : map.get(key)) {
556                                 parmList.add(key + "=" + value);
557                         }
558                 }
559                 String queryParams = String.join("&", parmList);
560                 if (!map.isEmpty()) {
561                         path += params + queryParams;
562                 }
563
564                 return path;
565
566         }
567
568         /**
569          * Handle writes.
570          *
571          * @param mediaType the media type
572          * @param method the method
573          * @param content the content
574          * @param versionParam the version param
575          * @param uri the uri
576          * @param headers the headers
577          * @param info the info
578          * @return the response
579          */
580         private Response handleWrites(MediaType mediaType, HttpMethod method, String content, String versionParam, String uri, HttpHeaders headers, UriInfo info) {
581
582                 Response response;
583                 TransactionalGraphEngine dbEngine = null;
584                 Loader loader;
585                 SchemaVersion version;
586                 String sourceOfTruth = headers.getRequestHeaders().getFirst("X-FromAppId");
587                 String transId = headers.getRequestHeaders().getFirst("X-TransactionId");
588                 boolean success = true;
589
590                 try {
591                         validateRequest(info);
592
593                         version = new SchemaVersion(versionParam);
594
595                         HttpEntry traversalUriHttpEntry = SpringContextAware.getBean("traversalUriHttpEntry", HttpEntry.class);
596                         traversalUriHttpEntry.setHttpEntryProperties(version);
597                         loader = traversalUriHttpEntry.getLoader();
598                         dbEngine = traversalUriHttpEntry.getDbEngine();
599                         URI uriObject = UriBuilder.fromPath(uri).build();
600                         this.validateURI(uriObject);
601                         QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject);
602                         String objName = uriQuery.getResultType();
603                         if (content.length() == 0) {
604                                 if (mediaType.toString().contains(MediaType.APPLICATION_JSON)) {
605                                         content = "{}";
606                                 } else {
607                                         content = "<empty/>";
608                                 }
609                         }
610                         Introspector obj = loader.unmarshal(objName, content, org.onap.aai.restcore.MediaType.getEnum(this.getInputMediaType(mediaType)));
611                         if (obj == null) {
612                                 throw new AAIException("AAI_3000", "object could not be unmarshalled:" + content);
613                         }
614
615                         if (mediaType.toString().contains(MediaType.APPLICATION_XML) && !content.equals("<empty/>") && isEmptyObject(obj)) {
616                                 throw new AAIInvalidXMLNamespace(content);
617                         }
618
619                         this.validateIntrospector(obj, loader, uriObject, method);
620
621                         DBRequest request =
622                                         new DBRequest.Builder(method, uriObject, uriQuery, obj, headers, info, transId)
623                                                         .rawRequestContent(content).build();
624                         List<DBRequest> requests = new ArrayList<>();
625                         requests.add(request);
626                         Pair<Boolean, List<Pair<URI, Response>>> responsesTuple  = traversalUriHttpEntry.process(requests,  sourceOfTruth);
627
628                         response = responsesTuple.getValue1().get(0).getValue1();
629                         success = responsesTuple.getValue0();
630                 } catch (AAIException e) {
631                         response = consumerExceptionResponseGenerator(headers, info, method, e);
632                         success = false;
633                 } catch (Exception e ) {
634                         AAIException ex = new AAIException("AAI_4000", e);
635                         response = consumerExceptionResponseGenerator(headers, info, method, ex);
636                         success = false;
637                 } finally {
638                         if (dbEngine != null) {
639                                 if (success) {
640                                         dbEngine.commit();
641                                 } else {
642                                         dbEngine.rollback();
643                                 }
644                         }
645                 }
646
647                 return response;
648         }
649
650         private void validateURI(URI uri) throws AAIException {
651                 if (hasRelatedTo(uri)) {
652                         throw new AAIException("AAI_3010");
653                 }
654         }
655         private boolean hasRelatedTo(URI uri) {
656
657                 return uri.toString().contains("/" + RestTokens.COUSIN + "/");
658         }
659
660         protected boolean isEmptyObject(Introspector obj) {
661                 return "{}".equals(obj.marshal(false));
662         }
663 }