use of org.ballerinalang.connector.api.BallerinaConnectorException in project ballerina by ballerina-lang.
the class ResourceExecutor method execute.
/**
* This method will execute the resource, given required details.
* And it will use the callback to notify interested parties about the
* outcome of the execution.
*
* @param resource to be executed.
* @param responseCallback to notify.
* @param properties to be passed to context.
* @param tracer to be passed to context.
* @param bValues for parameters.
*/
public static void execute(Resource resource, CallableUnitCallback responseCallback, Map<String, Object> properties, Tracer tracer, BValue... bValues) throws BallerinaConnectorException {
if (resource == null || responseCallback == null) {
throw new BallerinaConnectorException("invalid arguments provided");
}
ResourceInfo resourceInfo = resource.getResourceInfo();
WorkerExecutionContext context = new WorkerExecutionContext(resourceInfo.getPackageInfo().getProgramFile());
if (properties != null) {
context.globalProps.putAll(properties);
if (properties.get(Constants.GLOBAL_TRANSACTION_ID) != null) {
context.setLocalTransactionInfo(new LocalTransactionInfo(properties.get(Constants.GLOBAL_TRANSACTION_ID).toString(), properties.get(Constants.TRANSACTION_URL).toString(), "2pc"));
}
}
BLangVMUtils.initServerConnectorTrace(context, resource, tracer);
BLangVMUtils.setServiceInfo(context, resourceInfo.getServiceInfo());
BLangFunctions.invokeCallable(resourceInfo, context, bValues, responseCallback);
}
use of org.ballerinalang.connector.api.BallerinaConnectorException in project ballerina by ballerina-lang.
the class HttpDispatcher method handleError.
protected static void handleError(HTTPCarbonMessage cMsg, Throwable throwable) {
String errorMsg = throwable.getMessage();
// bre log should contain bre stack trace, not the ballerina stack trace
breLog.error("error: " + errorMsg, throwable);
try {
HttpUtil.handleFailure(cMsg, new BallerinaConnectorException(errorMsg, throwable.getCause()));
} catch (Exception e) {
breLog.error("Cannot handle error using the error handler for: " + e.getMessage(), e);
}
}
use of org.ballerinalang.connector.api.BallerinaConnectorException in project ballerina by ballerina-lang.
the class HttpDispatcher method findResource.
/**
* This method finds the matching resource for the incoming request.
*
* @param httpCarbonMessage incoming message.
* @return matching resource.
*/
public static HttpResource findResource(HTTPServicesRegistry servicesRegistry, HTTPCarbonMessage httpCarbonMessage) {
HttpResource resource = null;
String protocol = (String) httpCarbonMessage.getProperty(HttpConstants.PROTOCOL);
if (protocol == null) {
throw new BallerinaConnectorException("protocol not defined in the incoming request");
}
try {
// Find the Service TODO can be improved
HttpService service = HttpDispatcher.findService(servicesRegistry, httpCarbonMessage);
if (service == null) {
throw new BallerinaConnectorException("no Service found to handle the service request");
// Finer details of the errors are thrown from the dispatcher itself, Ideally we shouldn't get here.
}
// Find the Resource
resource = HttpResourceDispatcher.findResource(service, httpCarbonMessage);
} catch (Throwable throwable) {
handleError(httpCarbonMessage, throwable);
}
return resource;
}
use of org.ballerinalang.connector.api.BallerinaConnectorException in project ballerina by ballerina-lang.
the class HttpDispatcher method getSignatureParameters.
public static BValue[] getSignatureParameters(HttpResource httpResource, HTTPCarbonMessage httpCarbonMessage) {
// TODO Think of keeping struct type globally rather than creating for each request
BStruct serviceEndpoint = BLangConnectorSPIUtil.createBStruct(httpResource.getBalResource().getResourceInfo().getServiceInfo().getPackageInfo().getProgramFile(), PROTOCOL_PACKAGE_HTTP, SERVICE_ENDPOINT);
BStruct connection = BLangConnectorSPIUtil.createBStruct(httpResource.getBalResource().getResourceInfo().getServiceInfo().getPackageInfo().getProgramFile(), PROTOCOL_PACKAGE_HTTP, HttpConstants.CONNECTION);
BStruct inRequest = BLangConnectorSPIUtil.createBStruct(httpResource.getBalResource().getResourceInfo().getServiceInfo().getPackageInfo().getProgramFile(), PROTOCOL_PACKAGE_HTTP, HttpConstants.REQUEST);
BStruct inRequestEntity = BLangConnectorSPIUtil.createBStruct(httpResource.getBalResource().getResourceInfo().getServiceInfo().getPackageInfo().getProgramFile(), org.ballerinalang.mime.util.Constants.PROTOCOL_PACKAGE_MIME, Constants.ENTITY);
BStruct mediaType = BLangConnectorSPIUtil.createBStruct(httpResource.getBalResource().getResourceInfo().getServiceInfo().getPackageInfo().getProgramFile(), org.ballerinalang.mime.util.Constants.PROTOCOL_PACKAGE_MIME, Constants.MEDIA_TYPE);
BStruct cacheControlStruct = BLangConnectorSPIUtil.createBStruct(httpResource.getBalResource().getResourceInfo().getServiceInfo().getPackageInfo().getProgramFile(), PROTOCOL_PACKAGE_HTTP, REQUEST_CACHE_CONTROL);
RequestCacheControlStruct requestCacheControl = new RequestCacheControlStruct(cacheControlStruct);
HttpUtil.enrichServiceEndpointInfo(serviceEndpoint, httpCarbonMessage, httpResource);
HttpUtil.enrichConnectionInfo(connection, httpCarbonMessage);
serviceEndpoint.setRefField(SERVICE_ENDPOINT_CONNECTION_INDEX, connection);
HttpUtil.enrichConnectionInfo(connection, httpCarbonMessage);
HttpUtil.populateInboundRequest(inRequest, inRequestEntity, mediaType, httpCarbonMessage, requestCacheControl);
SignatureParams signatureParams = httpResource.getSignatureParams();
BValue[] bValues = new BValue[signatureParams.getParamCount()];
bValues[0] = serviceEndpoint;
bValues[1] = inRequest;
if (signatureParams.getParamCount() == 2) {
return bValues;
}
Map<String, String> resourceArgumentValues = (Map<String, String>) httpCarbonMessage.getProperty(HttpConstants.RESOURCE_ARGS);
for (int i = 0; i < signatureParams.getPathParams().size(); i++) {
// No need for validation as validation already happened at deployment time,
// only string parameters can be found here.
String argumentValue = resourceArgumentValues.get(signatureParams.getPathParams().get(i).getVarName());
if (argumentValue != null) {
try {
argumentValue = URLDecoder.decode(argumentValue, "UTF-8");
} catch (UnsupportedEncodingException e) {
// we can simply ignore and send the value to application and let the
// application deal with the value.
}
}
bValues[i + 2] = new BString(argumentValue);
}
if (signatureParams.getEntityBody() == null) {
return bValues;
}
try {
bValues[bValues.length - 1] = populateAndGetEntityBody(httpResource, inRequest, inRequestEntity, signatureParams.getEntityBody().getVarType());
} catch (BallerinaException ex) {
httpCarbonMessage.setProperty(HttpConstants.HTTP_STATUS_CODE, HttpConstants.HTTP_BAD_REQUEST);
throw new BallerinaConnectorException("data binding failed: " + ex.getMessage());
} catch (IOException ex) {
throw new BallerinaException(ex.getMessage());
}
return bValues;
}
use of org.ballerinalang.connector.api.BallerinaConnectorException in project ballerina by ballerina-lang.
the class HttpResourceDispatcher method handleOptionsRequest.
private static void handleOptionsRequest(HTTPCarbonMessage cMsg, HttpService service) throws URITemplateException {
HTTPCarbonMessage response = HttpUtil.createHttpCarbonMessage(false);
if (cMsg.getHeader(HttpHeaderNames.ALLOW.toString()) != null) {
response.setHeader(HttpHeaderNames.ALLOW.toString(), cMsg.getHeader(HttpHeaderNames.ALLOW.toString()));
} else if (service.getBasePath().equals(cMsg.getProperty(HttpConstants.TO)) && !service.getAllAllowedMethods().isEmpty()) {
response.setHeader(HttpHeaderNames.ALLOW.toString(), DispatcherUtil.concatValues(service.getAllAllowedMethods(), false));
} else {
cMsg.setProperty(HttpConstants.HTTP_STATUS_CODE, 404);
throw new BallerinaConnectorException("no matching resource found for path : " + cMsg.getProperty(HttpConstants.TO) + " , method : " + "OPTIONS");
}
CorsHeaderGenerator.process(cMsg, response, false);
response.setProperty(HttpConstants.HTTP_STATUS_CODE, 200);
response.addHttpContent(new DefaultLastHttpContent());
HttpUtil.sendOutboundResponse(cMsg, response);
}
Aggregations