use of org.wso2.carbon.registry.core.Resource in project carbon-apimgt by wso2.
the class OAuth2Authenticator method getRestAPIResource.
/*
* This methos is used to get the rest api resource based on the api context
* @param Request
* @return String : api resource object
* @throws APIMgtSecurityException if resource could not be found.
* */
private String getRestAPIResource(Request request) throws APIMgtSecurityException {
// todo improve to get appname as a property in the Request
String path = (String) request.getProperty(APIConstants.REQUEST_URL);
String restAPIResource = null;
// this is publisher API so pick that API
try {
if (path.contains(RestApiConstants.REST_API_PUBLISHER_CONTEXT)) {
restAPIResource = RestApiUtil.getPublisherRestAPIResource();
} else if (path.contains(RestApiConstants.REST_API_STORE_CONTEXT)) {
restAPIResource = RestApiUtil.getStoreRestAPIResource();
} else if (path.contains(RestApiConstants.REST_API_ADMIN_CONTEXT)) {
restAPIResource = RestApiUtil.getAdminRestAPIResource();
} else if (path.contains(RestApiConstants.REST_API_ANALYTICS_CONTEXT)) {
restAPIResource = RestApiUtil.getAnalyticsRestAPIResource();
} else {
throw new APIMgtSecurityException("No matching Rest Api definition found for path:" + path);
}
} catch (APIManagementException e) {
throw new APIMgtSecurityException(e.getMessage(), ExceptionCodes.AUTH_GENERAL_ERROR);
}
return restAPIResource;
}
use of org.wso2.carbon.registry.core.Resource in project ballerina by ballerina-lang.
the class WebSocketServerConnectorListener method onMessage.
@Override
public void onMessage(WebSocketInitMessage webSocketInitMessage) {
HTTPCarbonMessage msg = new HTTPCarbonMessage(((DefaultWebSocketInitMessage) webSocketInitMessage).getHttpRequest());
Map<String, String> pathParams = new HashMap<>();
WebSocketService wsService = WebSocketDispatcher.findService(servicesRegistry, pathParams, webSocketInitMessage, msg);
BStruct serviceEndpoint = BLangConnectorSPIUtil.createBStruct(wsService.getResources()[0].getResourceInfo().getServiceInfo().getPackageInfo().getProgramFile(), PROTOCOL_PACKAGE_HTTP, WEBSOCKET_ENDPOINT);
BStruct serverConnector = WebSocketUtil.createAndGetBStruct(wsService.getResources()[0]);
serverConnector.addNativeData(WebSocketConstants.WEBSOCKET_MESSAGE, webSocketInitMessage);
serverConnector.addNativeData(WebSocketConstants.WEBSOCKET_SERVICE, wsService);
serviceEndpoint.setRefField(SERVICE_ENDPOINT_CONNECTION_INDEX, serverConnector);
serviceEndpoint.setRefField(3, new BMap());
serverConnector.addNativeData(WEBSOCKET_ENDPOINT, serviceEndpoint);
Map<String, String> upgradeHeaders = webSocketInitMessage.getHeaders();
BMap<String, BString> bUpgradeHeaders = new BMap<>();
upgradeHeaders.forEach((key, value) -> bUpgradeHeaders.put(key, new BString(value)));
serviceEndpoint.setRefField(4, bUpgradeHeaders);
Resource onUpgradeResource = wsService.getResourceByName(WebSocketConstants.RESOURCE_NAME_ON_UPGRADE);
if (onUpgradeResource != null) {
Semaphore semaphore = new Semaphore(0);
AtomicBoolean isResourceExeSuccessful = new AtomicBoolean(false);
BStruct inRequest = BLangConnectorSPIUtil.createBStruct(WebSocketUtil.getProgramFile(wsService.getResources()[0]), PROTOCOL_PACKAGE_HTTP, HttpConstants.REQUEST);
BStruct inRequestEntity = BLangConnectorSPIUtil.createBStruct(WebSocketUtil.getProgramFile(wsService.getResources()[0]), org.ballerinalang.mime.util.Constants.PROTOCOL_PACKAGE_MIME, Constants.ENTITY);
BStruct mediaType = BLangConnectorSPIUtil.createBStruct(WebSocketUtil.getProgramFile(wsService.getResources()[0]), org.ballerinalang.mime.util.Constants.PROTOCOL_PACKAGE_MIME, Constants.MEDIA_TYPE);
BStruct cacheControlStruct = BLangConnectorSPIUtil.createBStruct(WebSocketUtil.getProgramFile(wsService.getResources()[0]), PROTOCOL_PACKAGE_HTTP, REQUEST_CACHE_CONTROL);
RequestCacheControlStruct requestCacheControl = new RequestCacheControlStruct(cacheControlStruct);
HttpUtil.populateInboundRequest(inRequest, inRequestEntity, mediaType, msg, requestCacheControl);
List<ParamDetail> paramDetails = onUpgradeResource.getParamDetails();
BValue[] bValues = new BValue[paramDetails.size()];
bValues[0] = serviceEndpoint;
bValues[1] = inRequest;
WebSocketDispatcher.setPathParams(bValues, paramDetails, pathParams, 2);
Tracer tracer = TraceManagerWrapper.newTracer(null, false);
upgradeHeaders.entrySet().stream().filter(c -> c.getKey().startsWith(TraceConstants.TRACE_PREFIX)).forEach(e -> tracer.addProperty(e.getKey(), e.getValue()));
Executor.submit(onUpgradeResource, new CallableUnitCallback() {
@Override
public void notifySuccess() {
isResourceExeSuccessful.set(true);
semaphore.release();
}
@Override
public void notifyFailure(BStruct error) {
ErrorHandlerUtils.printError("error: " + BLangVMErrors.getPrintableStackTrace(error));
semaphore.release();
}
}, null, tracer, bValues);
try {
semaphore.acquire();
if (isResourceExeSuccessful.get() && !webSocketInitMessage.isCancelled() && !webSocketInitMessage.isHandshakeStarted()) {
WebSocketUtil.handleHandshake(wsService, null, serverConnector);
}
} catch (InterruptedException e) {
throw new BallerinaConnectorException("Connection interrupted during handshake");
}
} else {
WebSocketUtil.handleHandshake(wsService, null, serverConnector);
}
}
use of org.wso2.carbon.registry.core.Resource in project ballerina by ballerina-lang.
the class GlobalVarServicePkgTest method testChangeAndAccessGlobalVarInDiffPkg.
@Test(description = "Test change global var in different package and access it", enabled = false)
public void testChangeAndAccessGlobalVarInDiffPkg() {
HTTPTestRequest cMsgChange = MessageUtils.generateHTTPMessage("/globalvar-pkg/change-global-var-diff-pkg", "GET");
Services.invokeNew(result, cMsgChange);
HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage("/globalvar-second-pkg/get-changed-resource-level", "GET");
HTTPCarbonMessage response = Services.invokeNew(result, cMsg);
Assert.assertNotNull(response);
// Expected Json message : {"changeVarFloat":345432.454}
BJSON bJson = new BJSON(new HttpMessageDataStreamer(response).getInputStream());
Assert.assertEquals(bJson.value().get("changeVarFloat").asText(), "345432.454");
}
use of org.wso2.carbon.registry.core.Resource in project ballerina by ballerina-lang.
the class GlobalVarServiceTest method testAccessingChangedGlobalVarInAnotherResource.
@Test(description = "Test accessing changed global var in another resource in same service", enabled = false)
public void testAccessingChangedGlobalVarInAnotherResource() {
HTTPTestRequest cMsgChange = MessageUtils.generateHTTPMessage("/globalvar/change-resource-level", "GET");
Services.invokeNew(result, cMsgChange);
HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage("/globalvar/get-changed-resource-level", "GET");
HTTPCarbonMessage response = Services.invokeNew(result, cMsg);
Assert.assertNotNull(response);
// Expected Json message : {"glbVarFloatChange":77.87}
BJSON bJson = new BJSON(new HttpMessageDataStreamer(response).getInputStream());
Assert.assertEquals(bJson.value().get("glbVarFloatChange").asText(), "77.87");
}
use of org.wso2.carbon.registry.core.Resource in project ballerina by ballerina-lang.
the class GlobalVarServiceTest method testAccessingChangedGlobalVarInAnotherResourceInAnotherService.
@Test(description = "Test accessing changed global var in another resource in different service", enabled = false)
public void testAccessingChangedGlobalVarInAnotherResourceInAnotherService() {
HTTPTestRequest cMsgChange = MessageUtils.generateHTTPMessage("/globalvar/change-resource-level", "GET");
Services.invokeNew(result, cMsgChange);
HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage("/globalvar-second/get-changed-resource-level", "GET");
HTTPCarbonMessage response = Services.invokeNew(result, cMsg);
Assert.assertNotNull(response);
// Expected Json message : {"glbVarFloatChange":77.87}
BJSON bJson = new BJSON(new HttpMessageDataStreamer(response).getInputStream());
Assert.assertEquals(bJson.value().get("glbVarFloatChange").asText(), "77.87");
}
Aggregations