Adding JSON message format support for secure-websocket.

revert-dabc3590
sinthuja 8 years ago
parent 0f17b2e766
commit 4da1d5ec3a

@ -75,6 +75,8 @@ public class SuperTenantSubscriptionEndpoint extends SubscriptionEndpoint {
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
} else {
log.info("Failed to authorize the connection for the stream : "+ streamName+" , version : "+ version);
}
} else {
try {

@ -74,6 +74,12 @@ public class TenantSubscriptionEndpoint extends SubscriptionEndpoint {
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
} else {
try {
session.close(new CloseReason(CloseReason.CloseCodes.CANNOT_ACCEPT, "Unauthorized Access"));
} catch (IOException e) {
log.error("Failed to disconnect the unauthorized client.", e);
}
}
} else {
try {

@ -20,11 +20,14 @@ package org.wso2.carbon.device.mgt.output.adapter.websocket;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONException;
import org.json.JSONObject;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.databridge.commons.Attribute;
import org.wso2.carbon.databridge.commons.Event;
import org.wso2.carbon.databridge.commons.StreamDefinition;
import org.wso2.carbon.device.mgt.output.adapter.websocket.constants.WebsocketConstants;
import org.wso2.carbon.device.mgt.output.adapter.websocket.internal.UIEventAdaptorServiceDataHolder;
import org.wso2.carbon.device.mgt.output.adapter.websocket.util.UIEventAdapterConstants;
import org.wso2.carbon.device.mgt.output.adapter.websocket.util.WebSocketSessionRequest;
@ -40,6 +43,7 @@ import org.wso2.carbon.event.stream.core.exception.EventStreamConfigurationExcep
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.LinkedBlockingDeque;
@ -196,13 +200,13 @@ public class UIEventAdapter implements OutputEventAdapter {
@Override
public void publish(Object message, Map<String, String> dynamicProperties) {
Event event = (Event) message;
StringBuilder eventBuilder = new StringBuilder("[");
String eventString;
if (streamSpecificEvents.size() == queueSize) {
streamSpecificEvents.removeFirst();
}
if (message instanceof Event) {
Event event = (Event) message;
StringBuilder eventBuilder = new StringBuilder("[");
eventBuilder.append(event.getTimeStamp());
if (event.getMetaData() != null) {
@ -247,14 +251,18 @@ public class UIEventAdapter implements OutputEventAdapter {
}
eventBuilder.append("]");
String eventString = eventBuilder.toString();
eventString = eventBuilder.toString();
} else {
eventString = message.toString();
}
Object[] eventValues = new Object[UIEventAdapterConstants.INDEX_TWO];
eventValues[UIEventAdapterConstants.INDEX_ZERO] = eventString;
eventValues[UIEventAdapterConstants.INDEX_ONE] = System.currentTimeMillis();
streamSpecificEvents.add(eventValues);
// fetch all valid sessions checked against any queryParameters provided when subscribing.
CopyOnWriteArrayList<WebSocketSessionRequest> validSessions = getValidSessions(event);
CopyOnWriteArrayList<WebSocketSessionRequest> validSessions = getValidSessions(message);
try {
executorService.execute(new WebSocketSender(validSessions, eventString));
@ -327,7 +335,7 @@ public class UIEventAdapter implements OutputEventAdapter {
* @param event the current event received and that which needs to be published to subscribed sessions.
* @return a list of all validated web-socket sessions against the queryString values.
*/
private CopyOnWriteArrayList<WebSocketSessionRequest> getValidSessions(Event event) {
private CopyOnWriteArrayList<WebSocketSessionRequest> getValidSessions(Object event) {
CopyOnWriteArrayList<WebSocketSessionRequest> validSessions = new CopyOnWriteArrayList<>();
UIOutputCallbackControllerServiceImpl uiOutputCallbackControllerServiceImpl =
UIEventAdaptorServiceDataHolder.getUIOutputCallbackRegisterServiceImpl();
@ -336,7 +344,12 @@ public class UIEventAdapter implements OutputEventAdapter {
uiOutputCallbackControllerServiceImpl.getSessions(tenantId, streamId);
if (webSocketSessionUtils != null) {
for (WebSocketSessionRequest webSocketSessionUtil : webSocketSessionUtils) {
boolean isValidSession = validateEventAgainstSessionFilters(event, webSocketSessionUtil);
boolean isValidSession;
if (event instanceof Event) {
isValidSession = validateEventAgainstSessionFilters((Event) event, webSocketSessionUtil);
} else {
isValidSession = validateJsonMessageAgainstEventFilters(event.toString(), webSocketSessionUtil);
}
if (isValidSession) {
validSessions.add(webSocketSessionUtil);
}
@ -405,6 +418,36 @@ public class UIEventAdapter implements OutputEventAdapter {
return true;
}
private boolean validateJsonMessageAgainstEventFilters(String jsonMessage, WebSocketSessionRequest webSocketSessionRequest) {
Map<String, String> queryParamValuePairs = webSocketSessionRequest.getQueryParamValuePairs();
if (queryParamValuePairs != null && !queryParamValuePairs.isEmpty()) {
// fetch the different attribute values received as part of the current event.
Set<String> queryParams = queryParamValuePairs.keySet();
for (String aQueryParam : queryParams) {
if (!aQueryParam.equalsIgnoreCase(WebsocketConstants.TOKEN_PARAM)) {
try {
String queryValue = queryParamValuePairs.get(aQueryParam);
if (queryValue != null && !queryValue.trim().isEmpty()) {
JSONObject jsonObject = new JSONObject(jsonMessage);
String eventValue = jsonObject.getString(aQueryParam);
if (eventValue == null || !eventValue.equalsIgnoreCase(queryValue)) {
return false;
}
}
} catch (JSONException e) {
if (log.isDebugEnabled()) {
log.debug("Unable validate the stream filter properties for event : " + jsonMessage
+ " ", e);
}
return false;
}
}
}
}
return true;
}
private class WebSocketSender implements Runnable {
private String message;

@ -51,6 +51,7 @@ public class UIEventAdapterFactory extends OutputEventAdapterFactory {
public List<String> getSupportedMessageFormats() {
List<String> supportedMessageFormats = new ArrayList<String>();
supportedMessageFormats.add(MessageType.WSO2EVENT);
supportedMessageFormats.add(MessageType.JSON);
return supportedMessageFormats;
}

@ -32,4 +32,5 @@ public class WebsocketConstants {
public static final String TOKEN_VALIDATION_ENDPOINT_URL = "tokenValidationEndpoint";
public static final String USERNAME = "username";
public static final String PASSWORD = "password";
public static final String TOKEN_PARAM = "token";
}

Loading…
Cancel
Save