Update traversal from AJSC 2 to Spring Boot
[aai/traversal.git] / aai-traversal / src / main / java / org / onap / aai / interceptors / pre / RequestTransactionLogging.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright (C) 2017 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
21 package org.onap.aai.interceptors.pre;
22
23 import java.io.ByteArrayInputStream;
24 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.Random;
28 import java.util.UUID;
29
30 import javax.annotation.Priority;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.ws.rs.container.ContainerRequestContext;
33 import javax.ws.rs.container.ContainerRequestFilter;
34 import javax.ws.rs.container.PreMatching;
35 import javax.ws.rs.core.MediaType;
36
37 import org.glassfish.jersey.message.internal.ReaderWriter;
38 import org.glassfish.jersey.server.ContainerException;
39 import org.onap.aai.exceptions.AAIException;
40 import org.onap.aai.interceptors.AAIContainerFilter;
41 import org.onap.aai.interceptors.AAIHeaderProperties;
42 import org.onap.aai.util.AAIConfig;
43 import org.onap.aai.util.AAIConstants;
44 import org.onap.aai.util.HbaseSaltPrefixer;
45 import org.springframework.beans.factory.annotation.Autowired;
46
47 import com.google.gson.JsonObject;
48
49 @PreMatching
50 @Priority(AAIRequestFilterPriority.REQUEST_TRANS_LOGGING)
51 public class RequestTransactionLogging extends AAIContainerFilter implements ContainerRequestFilter {
52
53         @Autowired
54         private HttpServletRequest httpServletRequest;
55
56         @Override
57         public void filter(ContainerRequestContext requestContext) throws IOException {
58
59                 String currentTimeStamp = genDate();
60                 String fullId = this.getAAITxIdToHeader(currentTimeStamp);
61                 this.addToRequestContext(requestContext, AAIHeaderProperties.AAI_TX_ID, fullId);
62                 this.addToRequestContext(requestContext, AAIHeaderProperties.AAI_REQUEST, this.getRequest(requestContext, fullId));
63                 this.addToRequestContext(requestContext, AAIHeaderProperties.AAI_REQUEST_TS, currentTimeStamp);
64         }
65
66         private void addToRequestContext(ContainerRequestContext requestContext, String name, String aaiTxIdToHeader) {
67                 requestContext.setProperty(name, aaiTxIdToHeader);
68         }
69
70         private String getAAITxIdToHeader(String currentTimeStamp) {
71                 String txId = UUID.randomUUID().toString();
72                 try {
73                         txId = HbaseSaltPrefixer.getInstance().prependSalt(AAIConfig.get(AAIConstants.AAI_NODENAME) + "-"
74                                         + currentTimeStamp + "-" + new Random(System.currentTimeMillis()).nextInt(99999));
75                 } catch (AAIException e) {
76                 }
77
78                 return txId;
79         }
80
81         private String getRequest(ContainerRequestContext requestContext, String fullId) {
82
83                 JsonObject request = new JsonObject();
84                 request.addProperty("ID", fullId);
85                 request.addProperty("Http-Method", requestContext.getMethod());
86                 String contentType = httpServletRequest.getContentType();
87
88                 if(contentType == null){
89                         contentType = MediaType.APPLICATION_JSON;
90                         requestContext.getHeaders().add("Content-Type", contentType);
91                 }
92
93                 request.addProperty("Content-Type", contentType);
94                 request.addProperty("Headers", requestContext.getHeaders().toString());
95
96                 ByteArrayOutputStream out = new ByteArrayOutputStream();
97                 InputStream in = requestContext.getEntityStream();
98
99                 try {
100                         if (in.available() > 0) {
101                                 ReaderWriter.writeTo(in, out);
102                                 byte[] requestEntity = out.toByteArray();
103                                 request.addProperty("Payload", new String(requestEntity, "UTF-8"));
104                                 requestContext.setEntityStream(new ByteArrayInputStream(requestEntity));
105                         }
106                 } catch (IOException ex) {
107                         throw new ContainerException(ex);
108                 }
109
110                 return request.toString();
111         }
112
113 }