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=========================================================
21 package org.onap.aai.schemaservice.interceptors.pre;
23 import com.google.gson.JsonObject;
25 import java.io.ByteArrayInputStream;
26 import java.io.ByteArrayOutputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.util.UUID;
31 import javax.annotation.Priority;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.ws.rs.container.ContainerRequestContext;
34 import javax.ws.rs.container.ContainerRequestFilter;
35 import javax.ws.rs.container.PreMatching;
36 import javax.ws.rs.core.MediaType;
38 import org.glassfish.jersey.message.internal.ReaderWriter;
39 import org.glassfish.jersey.server.ContainerException;
40 import org.onap.aai.schemaservice.interceptors.AAIContainerFilter;
41 import org.onap.aai.schemaservice.interceptors.AAIHeaderProperties;
42 import org.springframework.util.StringUtils;
43 import org.springframework.web.context.request.RequestAttributes;
44 import org.springframework.web.context.request.RequestContextHolder;
45 import org.springframework.web.context.request.ServletRequestAttributes;
48 @Priority(AAIRequestFilterPriority.REQUEST_TRANS_LOGGING)
49 public class RequestTransactionLogging extends AAIContainerFilter
50 implements ContainerRequestFilter {
52 private static final String DEFAULT_CONTENT_TYPE = MediaType.APPLICATION_JSON;
53 private static final String DEFAULT_RESPONSE_TYPE = MediaType.APPLICATION_XML;
54 private static final String CONTENT_TYPE = "Content-Type";
55 private static final String ACCEPT = "Accept";
56 private static final String TEXT_PLAIN = "text/plain";
59 public void filter(ContainerRequestContext requestContext) {
61 String currentTimeStamp = genDate();
62 String fullId = this.getAAITxIdToHeader();
63 this.addToRequestContext(requestContext, AAIHeaderProperties.AAI_TX_ID, fullId);
64 this.addToRequestContext(requestContext, AAIHeaderProperties.AAI_REQUEST,
65 this.getRequest(requestContext, fullId));
66 this.addToRequestContext(requestContext, AAIHeaderProperties.AAI_REQUEST_TS,
68 this.addDefaultContentType(requestContext);
71 private void addToRequestContext(ContainerRequestContext requestContext, String name,
72 String aaiTxIdToHeader) {
73 requestContext.setProperty(name, aaiTxIdToHeader);
76 private void addDefaultContentType(ContainerRequestContext requestContext) {
78 String contentType = requestContext.getHeaderString(CONTENT_TYPE);
79 String acceptType = requestContext.getHeaderString(ACCEPT);
81 if (contentType == null || contentType.contains(TEXT_PLAIN)) {
82 requestContext.getHeaders().putSingle(CONTENT_TYPE, DEFAULT_CONTENT_TYPE);
85 if (StringUtils.isEmpty(acceptType) || acceptType.contains(TEXT_PLAIN)) {
86 requestContext.getHeaders().putSingle(ACCEPT, DEFAULT_RESPONSE_TYPE);
90 private String getAAITxIdToHeader() {
91 String txId = UUID.randomUUID().toString();
95 private String getHttpServletRequestContentType() {
96 final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
97 if (requestAttributes != null) {
98 HttpServletRequest request =
99 ((ServletRequestAttributes) requestAttributes).getRequest();
100 return request.getContentType();
105 private String getRequest(ContainerRequestContext requestContext, String fullId) {
107 JsonObject request = new JsonObject();
108 request.addProperty("ID", fullId);
109 request.addProperty("Http-Method", requestContext.getMethod());
110 request.addProperty(CONTENT_TYPE, getHttpServletRequestContentType());
111 request.addProperty("Headers", requestContext.getHeaders().toString());
113 ByteArrayOutputStream out = new ByteArrayOutputStream();
114 InputStream in = requestContext.getEntityStream();
117 if (in.available() > 0) {
118 ReaderWriter.writeTo(in, out);
119 byte[] requestEntity = out.toByteArray();
120 request.addProperty("Payload", new String(requestEntity, "UTF-8"));
121 requestContext.setEntityStream(new ByteArrayInputStream(requestEntity));
123 } catch (IOException ex) {
124 throw new ContainerException(ex);
127 return request.toString();