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);
}
}
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;
}
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;
}
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();
}
}
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());
}
}
Aggregations