Search in sources :

Example 1 with ClientResource

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

the class Client method main.

/**
 * @param args
 */
public static void main(String[] args) {
    try {
        // ClientResource resource = new ClientResource("http://restlet.org");
        // 
        // // Customize the referrer property
        // resource.setReferrerRef("http://www.mysite.org");
        // 
        // // Write the response entity on the console
        // resource.get().write(System.out);
        // Prepare the request
        ClientResource resource = new ClientResource("http://localhost:8182/");
        // Add the client authentication to the call
        ChallengeScheme scheme = ChallengeScheme.HTTP_BASIC;
        ChallengeResponse authentication = new ChallengeResponse(scheme, "scott", "tiger");
        resource.setChallengeResponse(authentication);
        // Send the HTTP GET request
        resource.get();
        if (resource.getStatus().isSuccess()) {
            // Output the response entity on the JVM console
            resource.getResponseEntity().write(System.out);
        } else if (resource.getStatus().equals(Status.CLIENT_ERROR_UNAUTHORIZED)) {
            // Unauthorized access
            System.out.println("Access authorized by the server, check your credentials");
        } else {
            // Unexpected status
            System.out.println("An unexpected status was returned: " + resource.getStatus());
        }
    } catch (Throwable e) {
        e.printStackTrace(System.out);
    }
}
Also used : ChallengeScheme(org.restlet.data.ChallengeScheme) ClientResource(org.restlet.resource.ClientResource) ChallengeResponse(org.restlet.data.ChallengeResponse)

Example 2 with ClientResource

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

the class SimDataValuesServerResource method get_html.

@Override
public Representation get_html() {
    VCellApiApplication application = ((VCellApiApplication) getApplication());
    User vcellUser = application.getVCellUser(getChallengeResponse(), AuthenticationPolicy.ignoreInvalidCredentials);
    SimDataValuesRepresentation simDataValues = getSimDataValuesRepresentation(vcellUser);
    Map<String, Object> dataModel = new HashMap<String, Object>();
    // +"?"+VCellApiApplication.REDIRECTURL_FORMNAME+"="+getRequest().getResourceRef().toUrl());
    dataModel.put("loginurl", "/" + VCellApiApplication.LOGINFORM);
    dataModel.put("logouturl", "/" + VCellApiApplication.LOGOUT + "?" + VCellApiApplication.REDIRECTURL_FORMNAME + "=" + Reference.encode(getRequest().getResourceRef().toUrl().toString()));
    if (vcellUser != null) {
        dataModel.put("userid", vcellUser.getName());
    }
    dataModel.put("userId", getAttribute(PARAM_USER));
    dataModel.put("simId", getQueryValue(PARAM_SIM_ID));
    Long startRowParam = getLongQueryValue(PARAM_START_ROW);
    if (startRowParam != null) {
        dataModel.put("startRow", startRowParam);
    } else {
        dataModel.put("startRow", 1);
    }
    Long maxRowsParam = getLongQueryValue(PARAM_MAX_ROWS);
    if (maxRowsParam != null) {
        dataModel.put("maxRows", maxRowsParam);
    } else {
        dataModel.put("maxRows", 10);
    }
    dataModel.put("simdatavalues", simDataValues);
    int numVars = simDataValues.getVariables().length;
    if (numVars > 1) {
        StringBuffer buffer = new StringBuffer();
        String firstRow = "\"";
        for (int i = 0; i < numVars; i++) {
            firstRow += simDataValues.getVariables()[i].getName();
            if (i < numVars - 1) {
                firstRow += ",";
            }
        }
        firstRow += "\\n\" + \n";
        buffer.append(firstRow);
        int numTimes = simDataValues.getVariables()[0].getValues().length;
        for (int t = 0; t < numTimes; t++) {
            String row = "\"";
            for (int v = 0; v < numVars; v++) {
                row += simDataValues.getVariables()[v].getValues()[t];
                if (v < numVars - 1) {
                    row += ",";
                }
            }
            row += "\\n\"";
            if (t < numTimes - 1) {
                row += " + \n";
            }
            buffer.append(row);
        }
        String csvdata = buffer.toString();
        // String csvdata = "\"t,x,y\\n\" + \n" +
        // "\"0,0,0\\n\" + \n" +
        // "\"1,1,1\\n\" + \n" +
        // "\"2,2,4\\n\" + \n" +
        // "\"3,3,9\\n\" + \n" +
        // "\"4,4,16\\n\" + \n" +
        // "\"5,5,25\\n\"";
        dataModel.put("csvdata", csvdata);
    } else {
        return new StringRepresentation("<html>\n" + "<head>\n" + "No simulation data values found.\n" + "</body>\n" + "</html>", MediaType.TEXT_HTML);
    }
    Gson gson = new Gson();
    dataModel.put("jsonResponse", gson.toJson(simDataValues));
    Configuration templateConfiguration = application.getTemplateConfiguration();
    Representation formFtl = new ClientResource(LocalReference.createClapReference("/simdatavalues.ftl")).get();
    TemplateRepresentation templateRepresentation = new TemplateRepresentation(formFtl, templateConfiguration, dataModel, MediaType.TEXT_HTML);
    return templateRepresentation;
}
Also used : TemplateRepresentation(org.restlet.ext.freemarker.TemplateRepresentation) User(org.vcell.util.document.User) Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) Gson(com.google.gson.Gson) TemplateRepresentation(org.restlet.ext.freemarker.TemplateRepresentation) SimDataValuesRepresentation(org.vcell.rest.common.SimDataValuesRepresentation) StringRepresentation(org.restlet.representation.StringRepresentation) Representation(org.restlet.representation.Representation) SimDataValuesRepresentation(org.vcell.rest.common.SimDataValuesRepresentation) StringRepresentation(org.restlet.representation.StringRepresentation) VCellApiApplication(org.vcell.rest.VCellApiApplication) ClientResource(org.restlet.resource.ClientResource)

Example 3 with ClientResource

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

the class PublicationsServerResource method get_html.

@Override
public Representation get_html() {
    VCellApiApplication application = ((VCellApiApplication) getApplication());
    User vcellUser = application.getVCellUser(getChallengeResponse(), AuthenticationPolicy.ignoreInvalidCredentials);
    PublicationRepresentation[] publications = getPublicationRepresentations(vcellUser);
    Map<String, Object> dataModel = new HashMap<String, Object>();
    // +"?"+VCellApiApplication.REDIRECTURL_FORMNAME+"="+getRequest().getResourceRef().toUrl());
    dataModel.put("loginurl", "/" + VCellApiApplication.LOGINFORM);
    dataModel.put("logouturl", "/" + VCellApiApplication.LOGOUT + "?" + VCellApiApplication.REDIRECTURL_FORMNAME + "=" + Reference.encode(getRequest().getResourceRef().toUrl().toString()));
    if (vcellUser != null) {
        dataModel.put("userid", vcellUser.getName());
    }
    dataModel.put("pubId", getQueryValue(PARAM_PUB_ID));
    dataModel.put("orderBy", getQueryValue(PARAM_ORDERBY));
    dataModel.put("publications", Arrays.asList(publications));
    Gson gson = new Gson();
    dataModel.put("jsonResponse", gson.toJson(publications));
    Configuration templateConfiguration = application.getTemplateConfiguration();
    Representation formFtl = new ClientResource(LocalReference.createClapReference("/publications.ftl")).get();
    TemplateRepresentation templateRepresentation = new TemplateRepresentation(formFtl, templateConfiguration, dataModel, MediaType.TEXT_HTML);
    return templateRepresentation;
}
Also used : TemplateRepresentation(org.restlet.ext.freemarker.TemplateRepresentation) User(org.vcell.util.document.User) Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) Gson(com.google.gson.Gson) TemplateRepresentation(org.restlet.ext.freemarker.TemplateRepresentation) PublicationRepresentation(org.vcell.rest.common.PublicationRepresentation) Representation(org.restlet.representation.Representation) PublicationRepresentation(org.vcell.rest.common.PublicationRepresentation) VCellApiApplication(org.vcell.rest.VCellApiApplication) ClientResource(org.restlet.resource.ClientResource)

Example 4 with ClientResource

use of org.restlet.resource.ClientResource 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)

Example 5 with ClientResource

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

the class OptimizationServerResource method get_html.

@Override
public Representation get_html() {
    VCellApiApplication application = ((VCellApiApplication) getApplication());
    User vcellUser = application.getVCellUser(getChallengeResponse(), AuthenticationPolicy.ignoreInvalidCredentials);
    OptRun optRun = getOptRun(vcellUser);
    if (optRun == null) {
        throw new RuntimeException("optimization not found");
    }
    try {
        Map<String, Object> dataModel = new HashMap<String, Object>();
        // +"?"+VCellApiApplication.REDIRECTURL_FORMNAME+"="+getRequest().getResourceRef().toUrl());
        dataModel.put("loginurl", "/" + VCellApiApplication.LOGINFORM);
        dataModel.put("logouturl", "/" + VCellApiApplication.LOGOUT + "?" + VCellApiApplication.REDIRECTURL_FORMNAME + "=" + Reference.encode(getRequest().getResourceRef().toUrl().toString()));
        if (vcellUser != null) {
            dataModel.put("userid", vcellUser.getName());
        }
        dataModel.put("optId", getQueryValue(VCellApiApplication.OPTIMIZATIONID));
        TSerializer serializer = new TSerializer(new TJSONProtocol.Factory());
        String optRunJson = new String(serializer.serialize(optRun));
        dataModel.put("optimization", new JSONObject(optRunJson));
        dataModel.put("jsonResponse", optRunJson);
        Configuration templateConfiguration = application.getTemplateConfiguration();
        Representation formFtl = new ClientResource(LocalReference.createClapReference("/optimization.ftl")).get();
        TemplateRepresentation templateRepresentation = new TemplateRepresentation(formFtl, templateConfiguration, dataModel, MediaType.TEXT_HTML);
        return templateRepresentation;
    } catch (Exception e) {
        e.printStackTrace();
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e.getMessage());
    }
}
Also used : TemplateRepresentation(org.restlet.ext.freemarker.TemplateRepresentation) User(org.vcell.util.document.User) Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) TemplateRepresentation(org.restlet.ext.freemarker.TemplateRepresentation) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) Representation(org.restlet.representation.Representation) PermissionException(org.vcell.util.PermissionException) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) ResourceException(org.restlet.resource.ResourceException) TSerializer(org.apache.thrift.TSerializer) TJSONProtocol(org.apache.thrift.protocol.TJSONProtocol) JSONObject(org.json.JSONObject) OptRun(org.vcell.optimization.thrift.OptRun) VCellApiApplication(org.vcell.rest.VCellApiApplication) ClientResource(org.restlet.resource.ClientResource) JSONObject(org.json.JSONObject) ResourceException(org.restlet.resource.ResourceException)

Aggregations

ClientResource (org.restlet.resource.ClientResource)13 Representation (org.restlet.representation.Representation)12 Configuration (freemarker.template.Configuration)11 HashMap (java.util.HashMap)11 TemplateRepresentation (org.restlet.ext.freemarker.TemplateRepresentation)11 VCellApiApplication (org.vcell.rest.VCellApiApplication)11 User (org.vcell.util.document.User)11 Gson (com.google.gson.Gson)9 ResourceException (org.restlet.resource.ResourceException)4 StringRepresentation (org.restlet.representation.StringRepresentation)3 PublicationRepresentation (org.vcell.rest.common.PublicationRepresentation)3 ObjectNotFoundException (org.vcell.util.ObjectNotFoundException)3 PermissionException (org.vcell.util.PermissionException)3 JSONObject (org.json.JSONObject)2 JsonRepresentation (org.restlet.ext.json.JsonRepresentation)2 BiomodelReferenceRepresentation (org.vcell.rest.common.BiomodelReferenceRepresentation)2 BiomodelRepresentation (org.vcell.rest.common.BiomodelRepresentation)2 MathmodelReferenceRepresentation (org.vcell.rest.common.MathmodelReferenceRepresentation)2 KeyValue (org.vcell.util.document.KeyValue)2 BioModelReferenceRep (cbit.vcell.modeldb.BioModelReferenceRep)1