Search in sources :

Example 16 with ResourceException

use of org.restlet.resource.ResourceException in project OpenAM by OpenRock.

the class XacmlServiceTest method testImportXACMLNoPolicies.

@Test
public void testImportXACMLNoPolicies() throws Exception {
    //given
    Representation representation = mock(Representation.class);
    InputStream is = new ByteArrayInputStream("Hello World".getBytes());
    doReturn(is).when(representation).getStream();
    doReturn(Collections.emptyList()).when(importExport).importXacml(eq("/"), eq(is), any(Subject.class), eq(false));
    try {
        //when
        service.importXACML(representation);
        //then
        fail("Expect exception");
    } catch (ResourceException e) {
        assertThat(e.getStatus().getCode()).isEqualTo(BAD_REQUEST);
        assertThat(e.getMessage()).isEqualTo("No policies found in XACML document");
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JacksonRepresentation(org.restlet.ext.jackson.JacksonRepresentation) Representation(org.restlet.representation.Representation) ResourceException(org.restlet.resource.ResourceException) Subject(javax.security.auth.Subject) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 17 with ResourceException

use of org.restlet.resource.ResourceException in project lucene-solr by apache.

the class RestManager method getRestManager.

/**
   * Locates the RestManager using ThreadLocal SolrRequestInfo.
   */
public static RestManager getRestManager(SolrRequestInfo solrRequestInfo) {
    if (solrRequestInfo == null)
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, "No SolrRequestInfo in this Thread!");
    SolrQueryRequest req = solrRequestInfo.getReq();
    RestManager restManager = (req != null) ? req.getCore().getRestManager() : null;
    if (restManager == null)
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, "No RestManager found!");
    return restManager;
}
Also used : SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) ResourceException(org.restlet.resource.ResourceException)

Example 18 with ResourceException

use of org.restlet.resource.ResourceException in project vcell by virtualcell.

the class SimDataValuesServerResource method getSimDataValuesRepresentation.

private SimDataValuesRepresentation getSimDataValuesRepresentation(User vcellUser) {
    // if (!application.authenticate(getRequest(), getResponse())){
    // // not authenticated
    // return new SimulationTaskRepresentation[0];
    // }else{
    RestDatabaseService restDatabaseService = ((VCellApiApplication) getApplication()).getRestDatabaseService();
    try {
        DataSetTimeSeries dataSetTimeSeries = restDatabaseService.getDataSetTimeSeries(this, vcellUser);
        SimDataValuesRepresentation simDataRepresentation = new SimDataValuesRepresentation(dataSetTimeSeries);
        return simDataRepresentation;
    } catch (PermissionException e) {
        e.printStackTrace();
        throw new ResourceException(Status.CLIENT_ERROR_UNAUTHORIZED, "not authorized to stop simulation");
    } catch (ObjectNotFoundException e) {
        e.printStackTrace();
        throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "simulation not found");
    } catch (Exception e) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e.getMessage());
    }
// }
}
Also used : PermissionException(org.vcell.util.PermissionException) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) VCellApiApplication(org.vcell.rest.VCellApiApplication) ResourceException(org.restlet.resource.ResourceException) DataSetTimeSeries(cbit.vcell.simdata.DataSetTimeSeries) SimDataValuesRepresentation(org.vcell.rest.common.SimDataValuesRepresentation) PermissionException(org.vcell.util.PermissionException) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) ResourceException(org.restlet.resource.ResourceException)

Example 19 with ResourceException

use of org.restlet.resource.ResourceException in project vcell by virtualcell.

the class SimulationTasksServerResource method getSimulationTaskRepresentations.

private SimulationTaskRepresentation[] getSimulationTaskRepresentations(User vcellUser) {
    // if (!application.authenticate(getRequest(), getResponse())){
    // // not authenticated
    // return new SimulationTaskRepresentation[0];
    // }else{
    ArrayList<SimulationTaskRepresentation> simTaskReps = new ArrayList<SimulationTaskRepresentation>();
    RestDatabaseService restDatabaseService = ((VCellApiApplication) getApplication()).getRestDatabaseService();
    try {
        SimpleJobStatus[] simJobStatusList = restDatabaseService.query(this, vcellUser);
        for (SimpleJobStatus simpleJobStatus : simJobStatusList) {
            SimulationTaskRepresentation simTaskRep = new SimulationTaskRepresentation(simpleJobStatus);
            simTaskReps.add(simTaskRep);
        }
    } catch (PermissionException ee) {
        throw new ResourceException(Status.CLIENT_ERROR_UNAUTHORIZED, "permission denied to requested resource");
    } catch (DataAccessException e) {
        e.printStackTrace();
        throw new RuntimeException("failed to retrieve active jobs from VCell Database : " + e.getMessage());
    } catch (SQLException e) {
        e.printStackTrace();
        throw new RuntimeException("failed to retrieve active jobs from VCell Database : " + e.getMessage());
    }
    return simTaskReps.toArray(new SimulationTaskRepresentation[0]);
// }
}
Also used : SimulationTaskRepresentation(org.vcell.rest.common.SimulationTaskRepresentation) PermissionException(org.vcell.util.PermissionException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) VCellApiApplication(org.vcell.rest.VCellApiApplication) ResourceException(org.restlet.resource.ResourceException) DataAccessException(org.vcell.util.DataAccessException) SimpleJobStatus(cbit.vcell.server.SimpleJobStatus)

Example 20 with ResourceException

use of org.restlet.resource.ResourceException in project Xponents by OpenSextant.

the class XlayerClient method process.

/**
     * 
     * @param text
     * @return
     * @throws IOException
     * @throws JSONException
     */
public List<TextMatch> process(String docid, String text) throws IOException, JSONException {
    ClientResource client = new ClientResource(serviceContext, serviceURI);
    org.json.JSONObject content = new JSONObject();
    content.put("text", text);
    content.put("docid", docid);
    content.put("features", "places,coordinates,countries,persons,orgs,reverse-geocode");
    /* Coordinates mainly are XY locations; Reverse Geocode them to find what country the location resides */
    StringWriter data = new StringWriter();
    try {
        Representation repr = new JsonRepresentation(content.toString());
        repr.setCharacterSet(CharacterSet.UTF_8);
        //log.debug("CLIENT {} {}", serviceAddress, client);
        // Process and read response fully.
        Representation response = client.post(repr, MediaType.APPLICATION_JSON);
        response.write(data);
        response.exhaust();
        response.release();
        JSONObject json = new JSONObject(data.toString());
        log.debug("POST: response  {}", json.toString(2));
        JSONObject meta = json.getJSONObject("response");
        JSONArray annots = json.getJSONArray("annotations");
        List<TextMatch> matches = new ArrayList<TextMatch>();
        for (int x = 0; x < annots.length(); ++x) {
            Object m = annots.get(x);
            matches.add(Transforms.parseAnnotation(m));
        }
        return matches;
    } catch (ResourceException restErr) {
        if (restErr.getCause() instanceof IOException) {
            throw (IOException) restErr.getCause();
        } else {
            throw restErr;
        }
    } catch (java.net.SocketException err) {
        // This never happens. Restlet wraps everything.
        throw err;
    } finally {
        client.release();
    }
}
Also used : JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) Representation(org.restlet.representation.Representation) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) TextMatch(org.opensextant.extraction.TextMatch) IOException(java.io.IOException) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) StringWriter(java.io.StringWriter) ClientResource(org.restlet.resource.ClientResource) JSONObject(org.json.JSONObject) ResourceException(org.restlet.resource.ResourceException) JsonRepresentation(org.restlet.ext.json.JsonRepresentation)

Aggregations

ResourceException (org.restlet.resource.ResourceException)60 Representation (org.restlet.representation.Representation)19 VCellApiApplication (org.vcell.rest.VCellApiApplication)16 PermissionException (org.vcell.util.PermissionException)16 IOException (java.io.IOException)13 ObjectNotFoundException (org.vcell.util.ObjectNotFoundException)11 ArrayList (java.util.ArrayList)10 StringRepresentation (org.restlet.representation.StringRepresentation)9 Reference (org.restlet.data.Reference)8 Writer (java.io.Writer)7 WriterRepresentation (org.restlet.representation.WriterRepresentation)7 JSONObject (org.json.JSONObject)6 Response (org.restlet.Response)6 User (org.vcell.util.document.User)6 HashMap (java.util.HashMap)5 JsonRepresentation (org.restlet.ext.json.JsonRepresentation)5 EmptyRepresentation (org.restlet.representation.EmptyRepresentation)5 JSONException (org.json.JSONException)4 EntityReference (org.qi4j.api.entity.EntityReference)4 EntityTypeNotFoundException (org.qi4j.api.unitofwork.EntityTypeNotFoundException)4