2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.interceptors.pre;
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;
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;
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;
46 import com.google.gson.JsonObject;
49 @Priority(AAIRequestFilterPriority.REQUEST_TRANS_LOGGING)
50 public class RequestTransactionLogging extends AAIContainerFilter implements ContainerRequestFilter {
53 private HttpServletRequest httpServletRequest;
56 public void filter(ContainerRequestContext requestContext) throws IOException {
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);
65 private void addToRequestContext(ContainerRequestContext requestContext, String name, String aaiTxIdToHeader) {
66 requestContext.setProperty(name, aaiTxIdToHeader);
69 private String getAAITxIdToHeader(String currentTimeStamp) {
70 String txId = UUID.randomUUID().toString();
72 txId = HbaseSaltPrefixer.getInstance().prependSalt(AAIConfig.get(AAIConstants.AAI_NODENAME) + "-"
73 + currentTimeStamp + "-" + new Random(System.currentTimeMillis()).nextInt(99999));
74 } catch (AAIException e) {
80 private String getRequest(ContainerRequestContext requestContext, String fullId) {
82 JsonObject request = new JsonObject();
83 request.addProperty("ID", fullId);
84 request.addProperty("Http-Method", requestContext.getMethod());
85 String contentType = httpServletRequest.getContentType();
87 if(contentType == null){
88 contentType = MediaType.APPLICATION_JSON;
89 requestContext.getHeaders().add("Content-Type", contentType);
92 request.addProperty("Content-Type", contentType);
93 request.addProperty("Headers", requestContext.getHeaders().toString());
95 ByteArrayOutputStream out = new ByteArrayOutputStream();
96 InputStream in = requestContext.getEntityStream();
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));
105 } catch (IOException ex) {
106 throw new ContainerException(ex);
109 return request.toString();