本文整理汇总了Java中org.restlet.ext.json.JsonRepresentation类的典型用法代码示例。如果您正苦于以下问题:Java JsonRepresentation类的具体用法?Java JsonRepresentation怎么用?Java JsonRepresentation使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JsonRepresentation类属于org.restlet.ext.json包,在下文中一共展示了JsonRepresentation类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: post
import org.restlet.ext.json.JsonRepresentation; //导入依赖的package包/类
@Post
public Representation post(JsonRepresentation jsonRep) {
// handle request
JSONObject request = jsonRep.getJsonObject();
String username = request.getString("username");
long userscore = request.getLong("score");
System.out.println(username);
System.out.println(userscore);
Iterator userListIterator = Game.getInstance().getIterator();
while (userListIterator.hasNext()) {
User user = (User) userListIterator.next();
if (user.getUsername().equals(username)) {
user.setScore(userscore);
break;
}
}
// prepare and send response
JSONArray response = new JSONArray(Game.getInstance().getListOfUsers());
return new JsonRepresentation(response);
}
示例2: responseTokenRepresentation
import org.restlet.ext.json.JsonRepresentation; //导入依赖的package包/类
/**
* Response JSON document with valid token. The format of the JSON document
* is according to 5.1. Successful Response.
*
* @param token
* The token generated by the client.
* @param requestedScope
* The scope originally requested by the client.
* @return The token representation as described in RFC6749 5.1. <br>
* ※Local OAuth ではResultRepresentation型のポインタを返し、result=trueならアクセストークンを設定して返す。<br>
* アクセストークンは、getText()で返す。
* @throws ResourceException
*/
protected static Representation responseTokenRepresentation(Token token,
String[] requestedScope) throws JSONException {
JSONObject response = new JSONObject();
response.put(TOKEN_TYPE, token.getTokenType());
response.put(ACCESS_TOKEN, token.getAccessToken());
// response.put(EXPIRES_IN, token.getExpirePeriod());
String refreshToken = token.getRefreshToken();
if (refreshToken != null && !refreshToken.isEmpty()) {
response.put(REFRESH_TOKEN, refreshToken);
}
Scope[] scope = token.getScope();
if (!Scopes.isIdentical(Scope.toScopeStringArray(scope), requestedScope)) {
/*
* OPTIONAL, if identical to the scope requested by the client,
* otherwise REQUIRED. (5.1. Successful Response)
*/
response.put(SCOPE, Scopes.toString(scope));
}
return new JsonRepresentation(response);
}
示例3: receiveRequest
import org.restlet.ext.json.JsonRepresentation; //导入依赖的package包/类
/**
* Receives the JSON data sent by an external application
* <p>
* Pseudo Code<br/>
* 1. Receive the data<br/>
* 2. Extract the JSON array<br/>
* 3. Send the JSON array to saveData() for further processing<br/>
*
* @param entity
* @return Representation A JSON response is returned
*/
@Post("json:json")
public Representation receiveRequest(JsonRepresentation entity) {
counter.increment(endpoint);
logger.debug("Received request for posting a new data point.");
JSONArray jsonArr = null;
LocalDateTime currentDateTime = new LocalDateTime();
Response response = new Response();
Representation jsonResponse = new JsonRepresentation(response);
ResponseUtil util = new ResponseUtil();
try {
jsonArr = entity.getJsonArray();
} catch (JSONException e) {
logger.error("Error while trying to cast the data point: " + e.getMessage());
}
response = saveData(jsonArr, response);
response.setTimestamp(currentDateTime.toDateTime().toString());
jsonResponse = util.toJson(response);
return jsonResponse;
}
示例4: processQuery
import org.restlet.ext.json.JsonRepresentation; //导入依赖的package包/类
@Post
public Representation processQuery(Representation entity) throws IOException, JSONException {
JSONObject json = new JsonRepresentation(entity).getJsonObject();
if(!json.has("sql")) {
return errorRepresentation("SQL parameter not supplied");
}
List<JSONObject> list = Query.execute(json.getString("sql"), resultSet -> {
ArrayList<JSONObject> objList = new ArrayList<JSONObject>();
ResultSetMetaData metaData = resultSet.getMetaData();
while (resultSet.next()) {
JSONObject jsonObject = jsonForRow(resultSet, metaData);
objList.add(jsonObject);
}
return objList;
});
JSONArray array = new JSONArray(list);
return new JsonRepresentation(array);
}
示例5: getRequest
import org.restlet.ext.json.JsonRepresentation; //导入依赖的package包/类
@Post
public Representation getRequest(String strJson) {
logger.debug("IdExchange Request Start");
logger.debug("getData:{}", strJson);
IdExchangeRequestData idExReq = requestPaser(strJson);
if (null == idExReq) {
getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
return null;
}
logger.debug("terminationPoint:{}", idExReq.getTerminationPoint());
String ret = responseJson(PseudoMfData.getInstance().getIdExData()
.get("\"" + idExReq.getTerminationPoint() + "\""));
if (null == ret) {
getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
return null;
}
getResponse().setStatus(Status.SUCCESS_OK);
logger.debug("IdExchange Request End");
return new JsonRepresentation(new JSONObject(ret));
}
示例6: acceptRepresentation
import org.restlet.ext.json.JsonRepresentation; //导入依赖的package包/类
/**
* Handle a POST Http request. Create a new user
* We handle only a form request in this example. Other types could be
* JSON or XML.
*
* @param entity
* @throws ResourceException
*/
public void acceptRepresentation(Representation entity) throws ResourceException {
try {
if (entity.getMediaType().equals(MediaType.APPLICATION_WWW_FORM,
true)) {
AtomicReference<Form> form = new AtomicReference<Form>(new Form(entity));
SimpleUser u = new SimpleUser();
u.setName(form.get().getFirstValue("user[name]"));
getResponse().setStatus(Status.SUCCESS_OK);
// We are setting the representation in the example always to
// JSON.
// You could support multiple representation by using a
// parameter in the request like "?response_format=xml"
Representation rep = new JsonRepresentation(u.toJSON());
getResponse().setEntity(rep);
} else {
getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
}
} catch (Exception e) {
getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
}
}
示例7: describe
import org.restlet.ext.json.JsonRepresentation; //导入依赖的package包/类
@Get
public void describe() {
try {
JSONObject ret = new JSONObjectOrdered();
for (ServerTable tmd : mf.getTables()) {
TableId id = tmd.getId();
if (namespace != null) {
if (namespace.equals(id.getNamespace())) {
ret.put(id.getName(), tmd.getReference("table", getHostRef()));
}
} else {
if (!ret.has(id.getNamespace())) {
ret.put(id.getNamespace(), new JSONObjectOrdered());
}
JSONObject nso = (JSONObject) ret.get(id.getNamespace());
nso.put(id.getName(), tmd.getReference("table", getHostRef()));
}
}
getResponse().setEntity(new JsonRepresentation(ret));
} catch (JSONException ex) {
throw new ServerErrorException(ex);
}
}
示例8: info
import org.restlet.ext.json.JsonRepresentation; //导入依赖的package包/类
@Get
public void info() {
try {
JSONObject q = new JSONObject();
q.put("allowed_functions", new JSONArray(CheckExpressionParser.getAllowedFunctions()));
JSONObject v = new JSONObject();
v.put("service", cfg.getServiceVersion());
v.put("resthub", cfg.getResthubVersion());
JSONObject o = new JSONObject();
o.put("query", q);
o.put("version", v);
getResponse().setEntity(new JsonRepresentation(o));
} catch (JSONException ex) {
throw new ServerErrorException(ex);
}
}
示例9: describe
import org.restlet.ext.json.JsonRepresentation; //导入依赖的package包/类
@Get
public void describe() {
try {
JSONObject ret = new JSONObject();
for (ServerTable tmd : mf.getBlacklist()) {
TableId id = tmd.getId();
if (namespace != null) {
if (namespace.equals(id.getNamespace())) {
ret.put(id.getName(), tmd.getReference("blacklist", getHostRef()));
}
} else {
if (!ret.has(id.getNamespace())) {
ret.put(id.getNamespace(), new JSONObject());
}
JSONObject nso = (JSONObject) ret.get(id.getNamespace());
nso.put(id.getName(), tmd.getReference("blacklist", getHostRef()));
}
}
getResponse().setEntity(new JsonRepresentation(ret));
} catch (JSONException ex) {
throw new ServerErrorException(ex);
}
}
示例10: execute
import org.restlet.ext.json.JsonRepresentation; //导入依赖的package包/类
private JSONObject execute(Operation operation,
String apiPath,
Coppia<String, Object>[] parms) throws Exception {
JSONStringer jsRequest = new JSONStringer();
jsRequest.object();
for (int i = 0; i < parms.length; i++) {
jsRequest.key(parms[i].x()).value(parms[i].y());
}
jsRequest.endObject();
Representation rep = new JsonRepresentation(jsRequest);
rep.setMediaType(MediaType.APPLICATION_JSON);
Representation representation = null;
JSONObject result = null;
try {
representation = operation.execute(getClientResource(gitRestUrl() + apiPath), rep);
result = new JSONObject(representation.getText());
} finally {
if(representation != null) {
representation.exhaust();
representation.release();
}
}
return result;
}
示例11: listJobsJson
import org.restlet.ext.json.JsonRepresentation; //导入依赖的package包/类
@Get("json")
public Representation listJobsJson() {
Iterator<ComputationJob> jobs = listJobs() ;
JSONArray result = new JSONArray() ;
while (jobs.hasNext()) {
ComputationJob job = jobs.next() ;
JSONObject jsonJob = jsonJob(job) ;
String href = getNamespace().jobRef(entryName, modelName, getResolver().getJobName(job.getId()), true).toString() ;
try {
jsonJob.putOpt("href", href) ;
} catch (JSONException e) {
throw new ResourceException(Status.SERVER_ERROR_INTERNAL, "Exception adding references to JSON: " + href + ".", e) ;
}
result.put(jsonJob) ;
}
setStatus(Status.SUCCESS_OK) ;
return new JsonRepresentation(result) ;
}
示例12: listModelsJson
import org.restlet.ext.json.JsonRepresentation; //导入依赖的package包/类
@Get("json")
public Representation listModelsJson() {
Iterator<ComputationalModel> models = listModels() ;
JSONArray result = new JSONArray() ;
while (models.hasNext()) {
ComputationalModel model = models.next() ;
JSONObject jsonModel = jsonModel(model) ;
String href = getNamespace().modelRef(entryName, getResolver().getModelName(model.getId()), true).toString() ;
String jobsRef = getNamespace().jobsRef(entryName, getResolver().getModelName(model.getId()), true).toString() ;
try {
jsonModel.putOpt("href", href) ;
jsonModel.putOpt("jobs", jobsRef) ;
} catch (JSONException e) {
throw new ResourceException(Status.SERVER_ERROR_INTERNAL, "Exception adding references to JSON: " + href + ".", e) ;
}
result.put(jsonModel) ;
}
setStatus(Status.SUCCESS_OK) ;
return new JsonRepresentation(result) ;
}
示例13: getComment
import org.restlet.ext.json.JsonRepresentation; //导入依赖的package包/类
/**
* Get the comment
* @return
*/
@Get
public Representation getComment() {
// Fill in the reply
JSONObject reply = new JSONObject();
RestrictedData restrictedData = new RestrictedData();
String comment = restrictedData.get(identifier, RestrictedData.COMMENT);
try {
reply.put("comment", comment);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JsonRepresentation output = new JsonRepresentation(reply);
return output;
}
示例14: postTaxonomyGetTree
import org.restlet.ext.json.JsonRepresentation; //导入依赖的package包/类
/**
*
*
* @return the nid of the created plugin, an empty string if it has not been
* created
*/
public static String postTaxonomyGetTree(String vocabularyNumber) {
ClientResource cr2 = new ClientResource(DRUPALPATH + "/rest/taxonomy_vocabulary/getTree");
String text = "{\"vid\":\"" + vocabularyNumber + "\"}";
cr2.setMethod(Method.POST);
Representation rep2 = cr2.post(new JsonRepresentation(text));
Response resp = cr2.getResponse();
String jsonResponse = "";
if (resp.getStatus().isSuccess()) {
try {
jsonResponse = resp.getEntity().getText();
LOG.info(jsonResponse);
return jsonResponse;
} catch (IOException e) {
LOG.error("IOException: {}", e.getMessage());
}
} else {
LOG.info(resp.getStatus().getName());
}
return "";
}
示例15: configure
import org.restlet.ext.json.JsonRepresentation; //导入依赖的package包/类
@Post
public void configure(final JsonRepresentation request) {
final ExtensionConfigurationItem item = new ExtensionConfigurationItem(request.getJsonObject());
final ExtensionTokenManager tokenManager = getTokenManager();
if (tokenManager != null) {
tokenManager.setAddresses(item.getHubBaseUrl(), item.getExtensionUrl(), item.getoAuthAuthorizeUrl(),
item.getoAuthTokenUrl());
} else {
getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
}
}