Commit 0d62cc90 authored by Komal Makkar's avatar Komal Makkar
Browse files

Adding handshake filter

parent ff502037
Pipeline #20443 failed with stages
in 18 seconds
......@@ -67,11 +67,6 @@ public class CredentialHeadersProvider implements FactoryBean<DpsHeaders> {
}
private DpsHeaders getDpsHeadersForPostPutPatch() throws Exception {
if(this.pubsubRequestBodyExtractor.isHandshakeRequest()) {
// The headers are not needed for the handshake requests.
return new DpsHeaders();
}
Map<String, String> attributes = this.pubsubRequestBodyExtractor.extractAttributesFromRequestBody();
try {
//extract headers from pubsub message
......@@ -80,7 +75,7 @@ public class CredentialHeadersProvider implements FactoryBean<DpsHeaders> {
attributes.put(DpsHeaders.AUTHORIZATION, authToken);
return DpsHeaders.createFromMap(attributes);
} catch (AppException e) {
throw new Exception("Failed to generate headers for register service.");
throw new Exception("Failed to generate headers for register service.", e);
}
}
}
\ No newline at end of file
......@@ -158,9 +158,8 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto
extractNotificationData(notificationRequest);
}
} catch (Exception e) {
logger.error("Invalid Event Grid Message. %s", e.getMessage());
throw new AppException(HttpStatus.BAD_REQUEST.value(), "Request payload parsing error",
"Unable to parse request payload.", "Request contents are null or empty");
"Unable to parse request payload.", "Request contents are null or empty", e);
}
}
return notificationRequest;
......
// Copyright © Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.opengroup.osdu.notification.provider.azure.util;
import org.opengroup.osdu.core.common.logging.ILogger;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.model.http.Request;
import org.opengroup.osdu.notification.di.RequestInfoExt;
import static org.opengroup.osdu.core.common.http.ResponseHeaders.STANDARD_RESPONSE_HEADERS;
import org.opengroup.osdu.notification.provider.interfaces.IPubsubHandshakeHandler;
import org.opengroup.osdu.notification.provider.interfaces.IPubsubRequestBodyExtractor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.time.Duration;
import static org.opengroup.osdu.core.common.http.ResponseHeaders.STANDARD_RESPONSE_HEADERS;
@Component
@Order(0)
public class HandshakeFilter implements Filter {
@Autowired
private IPubsubRequestBodyExtractor requestBodyExtractor;
@Autowired
private IPubsubHandshakeHandler handshakeHandler;
@Override
public void init(FilterConfig filterConfig) {
//do nothing
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
if(this.requestBodyExtractor.isHandshakeRequest()){
HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
httpServletResponse.setStatus(200);
httpServletResponse.setContentType(MediaType.APPLICATION_JSON_VALUE);
setResponseHeaders(httpServletResponse);
String handshakeResponse = this.handshakeHandler.getHandshakeResponse();
PrintWriter out = httpServletResponse.getWriter();
out.print(handshakeResponse);
return;
}
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() { }
private void setResponseHeaders(HttpServletResponse httpServletResponse) {
Map<String, List<Object>> standardHeaders = STANDARD_RESPONSE_HEADERS;
for (Map.Entry<String, List<Object>> header : standardHeaders.entrySet()) {
httpServletResponse.addHeader(header.getKey(), header.getValue().toString());
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment