Search in sources :

Example 1 with Result

use of jdk.incubator.sql2.Result in project carbon-identity-framework by wso2.

the class JSONResponseWriter method abstractResultToJSONObject.

/**
 * Private method to convert a given Balana <code>{@link AbstractResult}</code> to a <code>{@link JsonObject}</code>
 *
 * @param result <code>{@link AbstractResult}</code>
 * @return <code>{@link JsonObject}</code>
 * @throws ResponseWriteException <code>{@link ResponseWriteException}</code>
 */
private static JsonObject abstractResultToJSONObject(AbstractResult result) throws ResponseWriteException {
    JsonObject jsonResult = new JsonObject();
    // Decision property is mandatory, if not set throw error
    if (result.getDecision() == -1) {
        throw new ResponseWriteException(40031, "XACML Result should contain the Decision");
    }
    jsonResult.addProperty(EntitlementEndpointConstants.DECISION, AbstractResult.DECISIONS[result.getDecision()]);
    // If Status object is present, convert it
    if (result.getStatus() != null) {
        jsonResult.add(EntitlementEndpointConstants.STATUS, statusToJSONObject(result.getStatus()));
    }
    // If Obligations are present
    if (result.getObligations() != null && !result.getObligations().isEmpty()) {
        // can only get ObligationResult objects from balana
        JsonArray obligations = new JsonArray();
        for (ObligationResult obligation : result.getObligations()) {
            if (obligation instanceof Obligation) {
                obligations.add(obligationToJsonObject((Obligation) obligation));
            } else {
                obligations.add(new JsonPrimitive(obligation.encode()));
            }
        }
        jsonResult.add(EntitlementEndpointConstants.OBLIGATIONS, obligations);
    }
    // Do the same with attributes
    if (result.getAdvices() != null && !result.getAdvices().isEmpty()) {
        // can only get ObligationResult objects from balana
        JsonArray advices = new JsonArray();
        for (Advice advice : result.getAdvices()) {
            advices.add(adviceToJsonObject(advice));
        }
        jsonResult.add(EntitlementEndpointConstants.ASSOCIATED_ADVICE, advices);
    }
    // If includeInResponse=true, other attributes will be populated from here with the decision.
    if (((Result) result).getAttributes() != null && !((Result) result).getAttributes().isEmpty()) {
        Set<Attributes> attributes = ((Result) result).getAttributes();
        for (Attributes attribute : attributes) {
            switch(attribute.getCategory().toString()) {
                case EntitlementEndpointConstants.CATEGORY_ACTION_URI:
                    jsonResult.add(EntitlementEndpointConstants.CATEGORY_ACTION, getJsonObject(attribute));
                    break;
                case EntitlementEndpointConstants.CATEGORY_RESOURCE_URI:
                    jsonResult.add(EntitlementEndpointConstants.CATEGORY_RESOURCE, getJsonObject(attribute));
                    break;
                case EntitlementEndpointConstants.CATEGORY_ACCESS_SUBJECT_URI:
                    jsonResult.add(EntitlementEndpointConstants.CATEGORY_ACCESS_SUBJECT, getJsonObject(attribute));
                    break;
                case EntitlementEndpointConstants.CATEGORY_ENVIRONMENT_URI:
                    jsonResult.add(EntitlementEndpointConstants.CATEGORY_ENVIRONMENT, getJsonObject(attribute));
                    break;
                case EntitlementEndpointConstants.CATEGORY_RECIPIENT_SUBJECT_URI:
                    jsonResult.add(EntitlementEndpointConstants.CATEGORY_RECIPIENT_SUBJECT, getJsonObject(attribute));
                    break;
                case EntitlementEndpointConstants.CATEGORY_INTERMEDIARY_SUBJECT_URI:
                    jsonResult.add(EntitlementEndpointConstants.CATEGORY_INTERMEDIARY_SUBJECT, getJsonObject(attribute));
                    break;
                case EntitlementEndpointConstants.CATEGORY_CODEBASE_URI:
                    jsonResult.add(EntitlementEndpointConstants.CATEGORY_CODEBASE, getJsonObject(attribute));
                    break;
                case EntitlementEndpointConstants.CATEGORY_REQUESTING_MACHINE_URI:
                    jsonResult.add(EntitlementEndpointConstants.CATEGORY_REQUESTING_MACHINE, getJsonObject(attribute));
                    break;
                default:
                    jsonResult.add(attribute.getCategory().toString(), getJsonObject(attribute));
                    break;
            }
        }
    }
    return jsonResult;
}
Also used : JsonArray(com.google.gson.JsonArray) Obligation(org.wso2.balana.xacml3.Obligation) ResponseWriteException(org.wso2.carbon.identity.entitlement.endpoint.exception.ResponseWriteException) JsonPrimitive(com.google.gson.JsonPrimitive) ObligationResult(org.wso2.balana.ObligationResult) Attributes(org.wso2.balana.xacml3.Attributes) JsonObject(com.google.gson.JsonObject) Advice(org.wso2.balana.xacml3.Advice) AbstractResult(org.wso2.balana.ctx.AbstractResult) ObligationResult(org.wso2.balana.ObligationResult) Result(org.wso2.balana.ctx.xacml3.Result)

Example 2 with Result

use of jdk.incubator.sql2.Result in project carbon-identity-framework by wso2.

the class TestJSONResponseWriter method testWriteWithObligations.

@Test
public void testWriteWithObligations() throws URISyntaxException {
    List<AttributeAssignment> assignments = new ArrayList<>();
    String content = "Error: Channel request is not WEB.";
    URI type = new URI("http://www.w3.org/2001/XMLSchema#string");
    URI attributeId = new URI("urn:oasis:names:tc:xacml:3.0:example:attribute:text");
    AttributeAssignment attributeAssignment = new AttributeAssignment(attributeId, type, null, content, null);
    assignments.add(attributeAssignment);
    List<ObligationResult> obligationResults = new ArrayList<>();
    ObligationResult obligationResult = new Obligation(assignments, new URI("channel_ko"));
    obligationResults.add(obligationResult);
    List<String> codes = new ArrayList<>();
    codes.add("urn:oasis:names:tc:xacml:1.0:status:ok");
    AbstractResult abstractResult = new Result(1, new Status(codes), obligationResults, null, null);
    ResponseCtx responseCtx = new ResponseCtx(abstractResult);
    JSONResponseWriter jsonResponseWriter = new JSONResponseWriter();
    try {
        JsonObject jsonObject = jsonResponseWriter.write(responseCtx);
        assertNotNull("Failed to build the XACML json response", jsonObject.toString());
        assertFalse("Failed to build the XACML json response", jsonObject.entrySet().isEmpty());
        for (Map.Entry<String, JsonElement> jsonElementEntry : jsonObject.entrySet()) {
            if (jsonElementEntry.getKey().equals("Response")) {
                JsonArray jsonArray = (JsonArray) jsonElementEntry.getValue();
                assertEquals("Failed to build the XACML json response with correct evaluation", jsonArray.get(0).getAsJsonObject().get("Decision").getAsString(), "Deny");
            }
        }
    } catch (ResponseWriteException e) {
        assertNull("Failed to build the XACML response", e);
    }
}
Also used : AttributeAssignment(org.wso2.balana.ctx.AttributeAssignment) Status(org.wso2.balana.ctx.Status) Obligation(org.wso2.balana.xacml3.Obligation) ResponseWriteException(org.wso2.carbon.identity.entitlement.endpoint.exception.ResponseWriteException) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) URI(java.net.URI) ResponseCtx(org.wso2.balana.ctx.ResponseCtx) AbstractResult(org.wso2.balana.ctx.AbstractResult) ObligationResult(org.wso2.balana.ObligationResult) Result(org.wso2.balana.ctx.xacml3.Result) JsonArray(com.google.gson.JsonArray) ObligationResult(org.wso2.balana.ObligationResult) JsonElement(com.google.gson.JsonElement) AbstractResult(org.wso2.balana.ctx.AbstractResult) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 3 with Result

use of jdk.incubator.sql2.Result in project struts-examples by apache.

the class IndexAction method execute.

public Result execute() {
    return (Result) invocation -> {
        HttpServletResponse response = ServletActionContext.getResponse();
        response.getWriter().println("Hello!");
    };
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Result(com.opensymphony.xwork2.Result)

Example 4 with Result

use of jdk.incubator.sql2.Result in project geotoolkit by Geomatys.

the class WPS2Process method fillOutputs.

/**
 * Fill {@link ParameterValueGroup parameters} of the process using the WPS
 * {@link ExecuteResponse response}.
 *
 * @throws ProcessException if data conversion fails.
 */
private void fillOutputs(Object response) throws ProcessException {
    try {
        if (response == null) {
            // request the result from the server
            final GetResultRequest request = registry.getClient().createGetResult(jobId);
            request.setDebug(debug);
            request.setClientSecurity(security);
            response = request.getResponse();
        }
        if (response instanceof Result) {
            final Result result = (Result) response;
            for (DataOutput out : result.getOutput()) {
                fillOutputs(outputParameters, out);
            }
        } else if (response instanceof ExceptionResponse) {
            final ExceptionResponse report = (ExceptionResponse) response;
            throw new ProcessException("Exception when getting process result.", this, report.toException());
        }
    } catch (JAXBException ex) {
        Logger.getLogger(WPS2Process.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(WPS2Process.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : DataOutput(org.geotoolkit.wps.xml.v200.DataOutput) GetResultRequest(org.geotoolkit.wps.client.GetResultRequest) ProcessException(org.geotoolkit.process.ProcessException) DismissProcessException(org.geotoolkit.process.DismissProcessException) ExceptionResponse(org.geotoolkit.ows.xml.ExceptionResponse) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) Result(org.geotoolkit.wps.xml.v200.Result)

Example 5 with Result

use of jdk.incubator.sql2.Result in project geotoolkit by Geomatys.

the class WPS2Process method execute.

@Override
protected void execute() throws ProcessException {
    final Result result;
    try {
        if (jobId != null) {
            try {
                // It's an already running process we've got here. All we have to do
                // is checking its status, to ensure/wait its completion.
                result = checkResult(getStatus());
            } catch (JAXBException | IOException ex) {
                throw new ProcessException("Cannot get process status", this, ex);
            }
        } else {
            final ExecuteRequest exec = createRequest();
            exec.setDebug(debug);
            exec.setClientSecurity(security);
            result = sendExecuteRequest(exec);
        }
    } catch (InterruptedException e) {
        throw new DismissProcessException("Process interrupted while executing", this, e);
    }
    if (!isDimissed()) {
        fillOutputs(result);
    }
}
Also used : ExecuteRequest(org.geotoolkit.wps.client.ExecuteRequest) ProcessException(org.geotoolkit.process.ProcessException) DismissProcessException(org.geotoolkit.process.DismissProcessException) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) DismissProcessException(org.geotoolkit.process.DismissProcessException) Result(org.geotoolkit.wps.xml.v200.Result)

Aggregations

Test (org.junit.Test)60 Result (edu.stanford.CVC4.Result)35 Expr (edu.stanford.CVC4.Expr)32 SExpr (edu.stanford.CVC4.SExpr)32 CVC4.vectorExpr (edu.stanford.CVC4.vectorExpr)31 Rational (edu.stanford.CVC4.Rational)25 Session (jdk.incubator.sql2.Session)22 List (java.util.List)21 ArrayList (java.util.ArrayList)19 Result (com.opensymphony.xwork2.Result)18 DataSource (jdk.incubator.sql2.DataSource)18 CompletableFuture (java.util.concurrent.CompletableFuture)12 Flow (java.util.concurrent.Flow)10 TimeUnit (java.util.concurrent.TimeUnit)10 SqlException (jdk.incubator.sql2.SqlException)10 AfterClass (org.junit.AfterClass)10 BeforeClass (org.junit.BeforeClass)10 ArrayType (edu.stanford.CVC4.ArrayType)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 Collector (java.util.stream.Collector)8