2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.onap.aai.interceptors.pre;
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;
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;
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;
47 import com.google.gson.JsonObject;
50 @Priority(AAIRequestFilterPriority.REQUEST_TRANS_LOGGING)
51 public class RequestTransactionLogging extends AAIContainerFilter implements ContainerRequestFilter {
54 private HttpServletRequest httpServletRequest;
57 public void filter(ContainerRequestContext requestContext) throws IOException {
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);
66 private void addToRequestContext(ContainerRequestContext requestContext, String name, String aaiTxIdToHeader) {
67 requestContext.setProperty(name, aaiTxIdToHeader);
70 private String getAAITxIdToHeader(String currentTimeStamp) {
71 String txId = UUID.randomUUID().toString();
73 txId = HbaseSaltPrefixer.getInstance().prependSalt(AAIConfig.get(AAIConstants.AAI_NODENAME) + "-"
74 + currentTimeStamp + "-" + new Random(System.currentTimeMillis()).nextInt(99999));
75 } catch (AAIException e) {
81 private String getRequest(ContainerRequestContext requestContext, String fullId) {
83 JsonObject request = new JsonObject();
84 request.addProperty("ID", fullId);
85 request.addProperty("Http-Method", requestContext.getMethod());
86 String contentType = httpServletRequest.getContentType();
88 if(contentType == null){
89 contentType = MediaType.APPLICATION_JSON;
90 requestContext.getHeaders().add("Content-Type", contentType);
93 request.addProperty("Content-Type", contentType);
94 request.addProperty("Headers", requestContext.getHeaders().toString());
96 ByteArrayOutputStream out = new ByteArrayOutputStream();
97 InputStream in = requestContext.getEntityStream();
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));
106 } catch (IOException ex) {
107 throw new ContainerException(ex);
110 return request.toString();