fbf9fbe3f83e5c07f7ad12ddb3643cfd20cb4b67
[aai/schema-service.git] /
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.aai.schemaservice.interceptors.pre;
21
22 import com.google.gson.JsonObject;
23 import java.io.ByteArrayInputStream;
24 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.UUID;
28 import javax.annotation.Priority;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.ws.rs.container.ContainerRequestContext;
31 import javax.ws.rs.container.ContainerRequestFilter;
32 import javax.ws.rs.container.PreMatching;
33 import javax.ws.rs.core.MediaType;
34 import org.glassfish.jersey.message.internal.ReaderWriter;
35 import org.glassfish.jersey.server.ContainerException;
36 import org.onap.aai.schemaservice.interceptors.AAIContainerFilter;
37 import org.onap.aai.schemaservice.interceptors.AAIHeaderProperties;
38 import org.springframework.util.StringUtils;
39 import org.springframework.web.context.request.RequestAttributes;
40 import org.springframework.web.context.request.RequestContextHolder;
41 import org.springframework.web.context.request.ServletRequestAttributes;
42
43 @PreMatching
44 @Priority(AAIRequestFilterPriority.REQUEST_TRANS_LOGGING)
45 public class RequestTransactionLogging extends AAIContainerFilter implements ContainerRequestFilter {
46
47     private static final String DEFAULT_CONTENT_TYPE = MediaType.APPLICATION_JSON;
48     private static final String DEFAULT_RESPONSE_TYPE = MediaType.APPLICATION_XML;
49     private static final String CONTENT_TYPE = "Content-Type";
50     private static final String ACCEPT = "Accept";
51     private static final String TEXT_PLAIN = "text/plain";
52
53     @Override
54     public void filter(ContainerRequestContext requestContext) {
55
56         String currentTimeStamp = genDate();
57         String fullId = this.getAAITxIdToHeader();
58         this.addToRequestContext(requestContext, AAIHeaderProperties.AAI_TX_ID, fullId);
59         this.addToRequestContext(requestContext, AAIHeaderProperties.AAI_REQUEST, this.getRequest(requestContext, fullId));
60         this.addToRequestContext(requestContext, AAIHeaderProperties.AAI_REQUEST_TS, currentTimeStamp);
61         this.addDefaultContentType(requestContext);
62     }
63
64     private void addToRequestContext(ContainerRequestContext requestContext, String name, String aaiTxIdToHeader) {
65         requestContext.setProperty(name, aaiTxIdToHeader);
66     }
67
68     private void addDefaultContentType(ContainerRequestContext requestContext) {
69
70         String contentType = requestContext.getHeaderString(CONTENT_TYPE);
71         String acceptType = requestContext.getHeaderString(ACCEPT);
72
73         if (contentType == null || contentType.contains(TEXT_PLAIN)) {
74             requestContext.getHeaders().putSingle(CONTENT_TYPE, DEFAULT_CONTENT_TYPE);
75         }
76
77         if (StringUtils.isEmpty(acceptType) || acceptType.contains(TEXT_PLAIN)) {
78             requestContext.getHeaders().putSingle(ACCEPT, DEFAULT_RESPONSE_TYPE);
79         }
80     }
81
82     private String getAAITxIdToHeader() {
83         String txId = UUID.randomUUID().toString();
84         return txId;
85     }
86
87     private String getHttpServletRequestContentType() {
88         final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
89         if (requestAttributes != null) {
90             HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
91             return request.getContentType();
92         }
93         return null;
94     }
95
96     private String getRequest(ContainerRequestContext requestContext, String fullId) {
97
98         JsonObject request = new JsonObject();
99         request.addProperty("ID", fullId);
100         request.addProperty("Http-Method", requestContext.getMethod());
101         request.addProperty(CONTENT_TYPE, getHttpServletRequestContentType());
102         request.addProperty("Headers", requestContext.getHeaders().toString());
103
104         ByteArrayOutputStream out = new ByteArrayOutputStream();
105         InputStream in = requestContext.getEntityStream();
106
107         try {
108             if (in.available() > 0) {
109                 ReaderWriter.writeTo(in, out);
110                 byte[] requestEntity = out.toByteArray();
111                 request.addProperty("Payload", new String(requestEntity, "UTF-8"));
112                 requestContext.setEntityStream(new ByteArrayInputStream(requestEntity));
113             }
114         } catch (IOException ex) {
115             throw new ContainerException(ex);
116         }
117
118         return request.toString();
119     }
120
121 }