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