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.schemaservice.interceptors.pre;
22 import com.google.gson.JsonObject;
23 import org.glassfish.jersey.message.internal.ReaderWriter;
24 import org.glassfish.jersey.server.ContainerException;
25 import org.onap.aai.schemaservice.interceptors.AAIContainerFilter;
26 import org.onap.aai.schemaservice.interceptors.AAIHeaderProperties;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.util.StringUtils;
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 import java.io.ByteArrayInputStream;
37 import java.io.ByteArrayOutputStream;
38 import java.io.IOException;
39 import java.io.InputStream;
40 import java.util.UUID;
43 @Priority(AAIRequestFilterPriority.REQUEST_TRANS_LOGGING)
44 public class RequestTransactionLogging extends AAIContainerFilter implements ContainerRequestFilter {
46 private static final String DEFAULT_CONTENT_TYPE = MediaType.APPLICATION_JSON;
47 private static final String DEFAULT_RESPONSE_TYPE = MediaType.APPLICATION_XML;
48 private static final String CONTENT_TYPE = "Content-Type";
49 private static final String ACCEPT = "Accept";
50 private static final String TEXT_PLAIN = "text/plain";
52 private HttpServletRequest httpServletRequest;
55 public void filter(ContainerRequestContext requestContext) {
57 String currentTimeStamp = genDate();
58 String fullId = this.getAAITxIdToHeader();
59 this.addToRequestContext(requestContext, AAIHeaderProperties.AAI_TX_ID, fullId);
60 this.addToRequestContext(requestContext, AAIHeaderProperties.AAI_REQUEST, this.getRequest(requestContext, fullId));
61 this.addToRequestContext(requestContext, AAIHeaderProperties.AAI_REQUEST_TS, currentTimeStamp);
62 this.addDefaultContentType(requestContext);
65 private void addToRequestContext(ContainerRequestContext requestContext, String name, String aaiTxIdToHeader) {
66 requestContext.setProperty(name, aaiTxIdToHeader);
69 private void addDefaultContentType(ContainerRequestContext requestContext) {
71 String contentType = requestContext.getHeaderString(CONTENT_TYPE);
72 String acceptType = requestContext.getHeaderString(ACCEPT);
74 if (contentType == null || contentType.contains(TEXT_PLAIN)) {
75 requestContext.getHeaders().putSingle(CONTENT_TYPE, DEFAULT_CONTENT_TYPE);
78 if (StringUtils.isEmpty(acceptType) || acceptType.contains(TEXT_PLAIN)) {
79 requestContext.getHeaders().putSingle(ACCEPT, DEFAULT_RESPONSE_TYPE);
83 private String getAAITxIdToHeader() {
84 String txId = UUID.randomUUID().toString();
88 private String getRequest(ContainerRequestContext requestContext, String fullId) {
90 JsonObject request = new JsonObject();
91 request.addProperty("ID", fullId);
92 request.addProperty("Http-Method", requestContext.getMethod());
93 request.addProperty(CONTENT_TYPE, httpServletRequest.getContentType());
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();