Search in sources :

Example 16 with BallerinaConnectorException

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);
}
Also used : WorkerExecutionContext(org.ballerinalang.bre.bvm.WorkerExecutionContext) ResourceInfo(org.ballerinalang.util.codegen.ResourceInfo) BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) LocalTransactionInfo(org.ballerinalang.util.transactions.LocalTransactionInfo)

Example 17 with BallerinaConnectorException

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);
    }
}
Also used : BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) BString(org.ballerinalang.model.values.BString) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) IOException(java.io.IOException) BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 18 with BallerinaConnectorException

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;
}
Also used : BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) BString(org.ballerinalang.model.values.BString)

Example 19 with BallerinaConnectorException

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;
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) UnsupportedEncodingException(java.io.UnsupportedEncodingException) BString(org.ballerinalang.model.values.BString) IOException(java.io.IOException) RequestCacheControlStruct(org.ballerinalang.net.http.caching.RequestCacheControlStruct) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 20 with BallerinaConnectorException

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);
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent)

Aggregations

BallerinaConnectorException (org.ballerinalang.connector.api.BallerinaConnectorException)32 Struct (org.ballerinalang.connector.api.Struct)9 BString (org.ballerinalang.model.values.BString)9 BStruct (org.ballerinalang.model.values.BStruct)9 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)7 HashMap (java.util.HashMap)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 URITemplateException (org.ballerinalang.net.uri.URITemplateException)4 BValue (org.ballerinalang.model.values.BValue)3 HttpResource (org.ballerinalang.net.http.HttpResource)3 URI (java.net.URI)2 CallableUnitCallback (org.ballerinalang.bre.bvm.CallableUnitCallback)2 AnnAttrValue (org.ballerinalang.connector.api.AnnAttrValue)2 BLangConnectorSPIUtil (org.ballerinalang.connector.api.BLangConnectorSPIUtil)2 Executor (org.ballerinalang.connector.api.Executor)2 ParamDetail (org.ballerinalang.connector.api.ParamDetail)2 Resource (org.ballerinalang.connector.api.Resource)2