cfbd3643effb318ee6715226f18454d5b0111f0f
[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
21 package org.onap.aai.schemaservice.interceptors.pre;
22
23 import com.google.gson.JsonObject;
24
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;
30
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;
37
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;
46
47 @PreMatching
48 @Priority(AAIRequestFilterPriority.REQUEST_TRANS_LOGGING)
49 public class RequestTransactionLogging extends AAIContainerFilter
50     implements ContainerRequestFilter {
51
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";
57
58     @Override
59     public void filter(ContainerRequestContext requestContext) {
60
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,
67             currentTimeStamp);
68         this.addDefaultContentType(requestContext);
69     }
70
71     private void addToRequestContext(ContainerRequestContext requestContext, String name,
72         String aaiTxIdToHeader) {
73         requestContext.setProperty(name, aaiTxIdToHeader);
74     }
75
76     private void addDefaultContentType(ContainerRequestContext requestContext) {
77
78         String contentType = requestContext.getHeaderString(CONTENT_TYPE);
79         String acceptType = requestContext.getHeaderString(ACCEPT);
80
81         if (contentType == null || contentType.contains(TEXT_PLAIN)) {
82             requestContext.getHeaders().putSingle(CONTENT_TYPE, DEFAULT_CONTENT_TYPE);
83         }
84
85         if (StringUtils.isEmpty(acceptType) || acceptType.contains(TEXT_PLAIN)) {
86             requestContext.getHeaders().putSingle(ACCEPT, DEFAULT_RESPONSE_TYPE);
87         }
88     }
89
90     private String getAAITxIdToHeader() {
91         String txId = UUID.randomUUID().toString();
92         return txId;
93     }
94
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();
101         }
102         return null;
103     }
104
105     private String getRequest(ContainerRequestContext requestContext, String fullId) {
106
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());
112
113         ByteArrayOutputStream out = new ByteArrayOutputStream();
114         InputStream in = requestContext.getEntityStream();
115
116         try {
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));
122             }
123         } catch (IOException ex) {
124             throw new ContainerException(ex);
125         }
126
127         return request.toString();
128     }
129
130 }