Commit 11a0532b authored by Komal Makkar's avatar Komal Makkar
Browse files

intermediate changes

parent 599e1389
// 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.pubsub;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.notification.pubsub.IPubsubHandshakeHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Component
@Lazy
public class EventGridHandshakeHandler implements IPubsubHandshakeHandler {
private JsonObject root = null;
@Autowired
private HttpServletRequest request;
/**
* Extract Handshake response string form Handshake request.
*
* @return validation string
*/
@Override
public String getHandshakeResponse() {
if (this.root == null) {
this.root = this.extractRootJsonElementFromRequestBody();
}
String response = null;
try {
JsonObject data = (JsonObject) this.root.get("data");
String validationCode = data.get("validationCode").getAsString();
JsonObject jsonResponse = new JsonObject();
jsonResponse.addProperty("ValidationResponse", validationCode);
response = validationCode.toString();
} catch (Exception exception) {
throw new AppException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Request payload parsing error",
"Unable to parse request payload.", exception);
}
return response;
}
/**
* Utility method to extract root element from the request.
*
* @return Root JsonObject
*/
private JsonObject extractRootJsonElementFromRequestBody() {
try {
JsonParser jsonParser = new JsonParser();
BufferedReader reader = request.getReader();
Stream<String> lines = reader.lines();
String requestBody = lines.collect(Collectors.joining("\n"));
JsonArray requestArray = (JsonArray) jsonParser.parse(requestBody);
JsonElement rootElement = requestArray.get(0);
if (!(rootElement instanceof JsonObject)) {
throw new AppException(HttpStatus.BAD_REQUEST.value(), "RequestBody is not JsonObject.",
"Request Body should be JsonObject to be processed.");
}
return rootElement.getAsJsonObject();
} catch (IOException e) {
throw new AppException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Request payload parsing error",
"Unable to parse request payload.", e);
}
}
}
/*
* Copyright 2017-2020, Schlumberger
*
* 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.
*/
// 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.pubsub;
......@@ -41,8 +39,9 @@ import java.util.stream.Stream;
@Component
@RequestScope
public class PubsubRequestBodyExtractor implements IPubsubRequestBodyExtractor {
public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtractor {
private static final String INVALID_PUBSUB_MESSAGE = "Invalid pubsub message";
private static final String EVENTGRID_VALIDATION_EVENT = "Microsoft.EventGrid.SubscriptionValidationEvent";
private static final Gson GSON = new Gson();
private MessageContent messageContent;
private JsonObject root = null;
......@@ -68,24 +67,27 @@ public class PubsubRequestBodyExtractor implements IPubsubRequestBodyExtractor {
}
public String extractNotificationIdFromRequestBody() {
}
public boolean isHandshakeRequest() {
if (this.root == null) {
this.root = this.extractRootJsonElementFromRequestBody();
}
JsonElement subscription = this.root.get("subscription");
if (subscription == null) {
throw new AppException(HttpStatus.BAD_REQUEST.value(), INVALID_PUBSUB_MESSAGE, "subscription object not found");
try {
JsonElement data = this.root.get("eventType");
if (EVENTGRID_VALIDATION_EVENT.equals(data.getAsString())) {
return true;
}
} catch (Exception exception) {
throw new AppException(HttpStatus.BAD_REQUEST.value(), "RequestBody is not JsonObject.",
"Request Body should be JsonObject to be processed.");
}
String[] fullNotificationId = subscription.getAsString().split("/");
return fullNotificationId[fullNotificationId.length - 1];
}
public boolean isHandshakeRequest() {
return false;
}
private MessageContent extractPubsubMessageFromRequestBody() {
if (this.root == null) {
/* if (this.root == null) {
this.root = this.extractRootJsonElementFromRequestBody();
}
JsonElement message = this.root.get("message");
......@@ -114,7 +116,7 @@ public class PubsubRequestBodyExtractor implements IPubsubRequestBodyExtractor {
String decoded = new String(Base64.getDecoder().decode(data));
content.setData(decoded);
return content;
return content;*/
}
private JsonObject extractRootJsonElementFromRequestBody() {
......
package org.opengroup.osdu.notification.provider.azure.pubsub;
import org.opengroup.osdu.notification.pubsub.IPubsubHandshakeHandler;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
@Component
@Lazy
public class PubsubHandshakeHandler implements IPubsubHandshakeHandler {
@Override
public String getHandshakeResponse() {
return null;
}
}
......@@ -32,21 +32,21 @@ public class Config {
private static Config config = new Config();
public static Config Instance() {
String env = getEnvironment();
String env = "LOCAL";//getEnvironment();
config.ClientTenant = "nonexistenttenant";
config.IntegrationAudience = "245464679631-ktfdfpl147m1mjpbutl00b3cmffissgq.apps.googleusercontent.com";
config.OsduTenant = "opendes";
config.Topic = "records-changed";
config.hmacSecretValue = System.getProperty("HMAC_SECRET", System.getenv("HMAC_SECRET"));
config.hmacSecretValue = "asdf";//System.getProperty("HMAC_SECRET", System.getenv("HMAC_SECRET"));
if (env.equalsIgnoreCase("LOCAL")) {
//make sure to run register service on a different port. You can also choose to point to Register service that is running in cloud
String registerUrl = "http://localhost:8081/";
//must have notification and register services running on different ports
config.HostUrl = "http://localhost:8080/";
config.HostUrl = "http://localhost:8082/";
config.GSAPushUrl = registerUrl+"/test/gsa-challenge/";
config.HMACPushUrl = registerUrl+"/test/challenge/";
......
Supports Markdown
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