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 com.google.gson.JsonObject;
24 import java.io.ByteArrayInputStream;
25 import java.io.ByteArrayOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.nio.charset.Charset;
29 import java.security.SecureRandom;
30 import java.util.Random;
31 import java.util.UUID;
33 import javax.annotation.Priority;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.ws.rs.container.ContainerRequestContext;
36 import javax.ws.rs.container.ContainerRequestFilter;
37 import javax.ws.rs.container.PreMatching;
38 import javax.ws.rs.core.MediaType;
39 import javax.ws.rs.core.MultivaluedMap;
40 import javax.ws.rs.core.UriInfo;
42 import org.apache.commons.io.Charsets;
43 import org.apache.commons.io.IOUtils;
44 import org.glassfish.jersey.message.internal.ReaderWriter;
45 import org.glassfish.jersey.server.ContainerException;
46 import org.onap.aai.exceptions.AAIException;
47 import org.onap.aai.interceptors.AAIContainerFilter;
48 import org.onap.aai.interceptors.AAIHeaderProperties;
49 import org.onap.aai.util.AAIConfig;
50 import org.onap.aai.util.AAIConstants;
51 import org.onap.aai.util.HbaseSaltPrefixer;
52 import org.springframework.util.StringUtils;
56 @Priority(AAIRequestFilterPriority.REQUEST_TRANS_LOGGING)
57 public class RequestTransactionLogging extends AAIContainerFilter
58 implements ContainerRequestFilter {
60 private static final String DEFAULT_CONTENT_TYPE = MediaType.APPLICATION_JSON;
61 private static final String DEFAULT_RESPONSE_TYPE = MediaType.APPLICATION_XML;
63 private static final String CONTENT_TYPE = "Content-Type";
64 private static final String ACCEPT = "Accept";
65 private static final String TEXT_PLAIN = "text/plain";
66 private static final String WILDCARD = "*/*";
67 private static final String APPLICATION_JSON = "application/json";
70 public void filter(ContainerRequestContext requestContext) throws IOException {
71 String currentTimeStamp = genDate();
72 String fullId = this.getAAITxIdToHeader(currentTimeStamp);
73 requestContext.setProperty(AAIHeaderProperties.AAI_TX_ID, fullId);
74 requestContext.setProperty(AAIHeaderProperties.AAI_REQUEST, this.getRequest(requestContext, fullId));
75 requestContext.setProperty(AAIHeaderProperties.AAI_REQUEST_TS, currentTimeStamp);
76 this.addDefaultContentType(requestContext);
79 private void addDefaultContentType(ContainerRequestContext requestContext) {
81 String contentType = requestContext.getHeaderString(CONTENT_TYPE);
82 String acceptType = requestContext.getHeaderString(ACCEPT);
84 if (contentType == null || contentType.contains(TEXT_PLAIN)) {
85 requestContext.getHeaders().putSingle(CONTENT_TYPE, DEFAULT_CONTENT_TYPE);
88 if (WILDCARD.equals(acceptType) || StringUtils.isEmpty(acceptType)
89 || acceptType.contains(TEXT_PLAIN)) {
90 UriInfo uriInfo = requestContext.getUriInfo();
91 if (uriInfo != null) {
92 String path = uriInfo.getPath();
93 if (path.endsWith("/dsl") || path.endsWith("/query")
94 || path.contains("/recents/")) {
95 requestContext.getHeaders().putSingle(ACCEPT, APPLICATION_JSON);
97 requestContext.getHeaders().putSingle(ACCEPT, DEFAULT_RESPONSE_TYPE);
100 requestContext.getHeaders().putSingle(ACCEPT, DEFAULT_RESPONSE_TYPE);
105 private String getAAITxIdToHeader(String currentTimeStamp) {
106 String txId = UUID.randomUUID().toString();
108 Random rand = new SecureRandom();
109 int number = rand.nextInt(99999);
110 txId = HbaseSaltPrefixer.getInstance().prependSalt(
111 AAIConfig.get(AAIConstants.AAI_NODENAME) + "-" + currentTimeStamp + "-" + number); // new
112 // Random(System.currentTimeMillis()).nextInt(99999)
113 } catch (AAIException e) {
119 private String getRequest(ContainerRequestContext requestContext, String fullId) throws IOException {
121 JsonObject request = new JsonObject();
122 request.addProperty("ID", fullId);
123 request.addProperty("Http-Method", requestContext.getMethod());
124 request.addProperty("Headers", requestContext.getHeaders().toString());
126 // String requestEntity = IOUtils.toString(requestContext.getEntityStream(), Charsets.UTF_8);
127 // InputStream in = IOUtils.toInputStream(requestEntity, Charset.defaultCharset());
128 // requestContext.setEntityStream(in);
130 return request.toString();