@Override public Representation toRepresentation(Object source, Variant target, Resource resource) { Representation result = null; if (source instanceof JSONArray) { result = new JsonRepresentation((JSONArray) source); } else if (source instanceof JSONObject) { result = new JsonRepresentation((JSONObject) source); } else if (source instanceof JSONTokener) { result = new JsonRepresentation((JSONTokener) source); } return result; }
@Override public Representation toRepresentation(Object source, Variant target, Resource resource) { Representation result = null; if (source instanceof JSONArray) { result = new JsonRepresentation((JSONArray) source); } else if (source instanceof JSONObject) { result = new JsonRepresentation((JSONObject) source); } else if (source instanceof JSONTokener) { result = new JsonRepresentation((JSONTokener) source); } return result; }
protected Representation getRepresentation(PersistentObject po) throws ResourceException { StringBuilder json = new StringBuilder(); json.append("[{\"data\": ["); String poAsJson = po.toJson(); json.append(poAsJson.substring(0, poAsJson.length() - 1)); json.append(", \"row\": 1}"); json.append("], \"meta\" : {"); json.append("\"qualifiedName\": \"restAndJson::org::umlg::test::Human\""); json.append(", \"to\": "); json.append(po.getMetaDataAsJson()); json.append("}}]"); return new JsonRepresentation(json.toString()); }
protected Representation executeStatic(String query, String contextClassifierQualifiedName, String type) { if (type.equalsIgnoreCase("ocl")) { String result = UmlgOclExecutor.executeOclQueryAsJson(contextClassifierQualifiedName, query); return new JsonRepresentation(result); } else if (type.equalsIgnoreCase("groovy")) { String result = GroovyExecutor.INSTANCE.executeGroovyAsString(null, query); return new StringRepresentation(result); } else { throw new RuntimeException("Unknown query type " + type); } }
@Get public Representation toJson() throws JSONException { // Create a JSON object structure similar to a map JSONObject mailElt = new JSONObject(); mailElt.put("status", "received"); mailElt.put("subject", "Message to self"); mailElt.put("content", "Doh!"); mailElt.put("accountRef", new Reference(getReference(), "..") .getTargetRef().toString()); return new JsonRepresentation(mailElt); }
/** * Returns a JsonRepresentation of the layer * * @param layer * @return */ public JsonRepresentation getJsonRepresentation(TileLayer layer) { JsonRepresentation rep = null; try { XStream xs = xmlConfig.getConfiguredXStream(new XStream( new JsonHierarchicalStreamDriver())); JSONObject obj = new JSONObject(xs.toXML(layer)); rep = new JsonRepresentation(obj); } catch (JSONException jse) { jse.printStackTrace(); } return rep; }
protected Representation execute(String query, Object contextId, String type) { if (type.equalsIgnoreCase("ocl")) { UmlgNode context = UMLG.get().getEntity(contextId); // UMLG.get().executeQueryToJson(UmlgQueryEnum.OCL, context.getId(), query); return new JsonRepresentation(UmlgOclExecutor.executeOclQueryAsJson(context, query)); } else if (type.equalsIgnoreCase("groovy")) { String result = GroovyExecutor.INSTANCE.executeGroovyAsString(contextId, query); return new StringRepresentation(result); } else { throw new RuntimeException("Unknown query type " + type); } }
@Override protected Representation get() throws ResourceException { OIDCConfig config = accessManager.getOIDCConfig(); RsaJsonWebKey key = (RsaJsonWebKey)config.getSigningKey(); return new JsonRepresentation("{\"keys\":[" + key.toJson() + "]}"); } }
@Get("json") public JsonRepresentation represent() { Version v = new Version(); JsonRepresentation sr = new JsonRepresentation("{\"version\":\""+v.getVersion()+ "\"}"); sr.setMediaType(MediaType.APPLICATION_JSON); return sr; }
/** * Returns the representation of the given error. The format of the JSON * document is according to 5.2. Error Response. * * @param ex * Any OAuthException with error * @return The representation of the given error. */ public static Representation responseErrorRepresentation(OAuthException ex) { try { return new JsonRepresentation(ex.createErrorDocument()); } catch (JSONException e) { StringRepresentation r = new StringRepresentation( "{\"error\":\"server_error\",\"error_description:\":\"" + e.getLocalizedMessage() + "\"}"); r.setMediaType(MediaType.APPLICATION_JSON); return r; } }
@Get public Representation getMe() throws IOException, JSONException { Token token = (Token) getRequest().getAttributes().get( Token.class.getName()); if (token == null) { return new StringRepresentation("Token not found!"); } ProtectedClientResource me = new ProtectedClientResource( "https://graph.facebook.com/me"); me.setUseBodyMethod(true); me.setToken(token); JSONObject result = new JsonRepresentation(me.get()).getJsonObject(); return new StringRepresentation("Hello " + result.getString("name")); } }
@Post("json") public Representation translate(JsonRepresentation jsonRepresentation) throws ResourceException { try { // No alternate versions supported. But if we did: String version = (String) getRequest().getAttributes().get("version"); if ( version == null ) { version = LayerConfiguration.DEFAULT_VERSION; } JSONObject arguments = jsonRepresentation.getJsonObject(); return new JsonRepresentation( _service.getTranslation(arguments) ); } catch ( Exception e) { throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Unable to translate text. Check parameters.", e); } } /**
/** * Returns the representation of the given error. The format of the JSON * document is according to 5.2. Error Response. * * @param ex * Any OAuthException with error * @return The representation of the given error. */ public static Representation responseErrorRepresentation(OAuthException ex) { try { return new JsonRepresentation(ex.createErrorDocument()); } catch (JSONException e) { StringRepresentation r = new StringRepresentation( "{\"error\":\"server_error\",\"error_description:\":\"" + e.getLocalizedMessage() + "\"}"); r.setMediaType(MediaType.APPLICATION_JSON); return r; } }
@Get("json") public Representation getUserStatus() throws JSONException { User user = getRequest().getClientInfo().getUser(); getLogger().info("getUserStatus: " + user.getIdentifier()); SampleUser sampleUser = OAuth2Sample.getSampleUserManager() .findUserById(user.getIdentifier()); if (sampleUser == null) { setStatus(Status.CLIENT_ERROR_BAD_REQUEST); return null; } JSONObject result = new JSONObject(); Object status = sampleUser.getStatus(); if (status != null) { result.put("status", status); } else { result.put("status", ""); } return new JsonRepresentation(result); }
@Get public Representation getConfig() { try { String result = "{}"; String name = ( String ) getRequest().getAttributes().get( "name" ); File matchingFile = _service.findResourceConfig(name); if (matchingFile != null) { result = _service.replaceProperties(matchingFile); } setStatus(Status.SUCCESS_OK); return new JsonRepresentation( result ); } catch ( Exception e ) { throw new ResourceException( Status.SERVER_ERROR_INTERNAL, "Unable to get configuration", e ); } } }
/** * GET request. Returns a JSON response from the translation service specified */ @Get public Representation translate() throws ResourceException { // get the params from try { // No alternate versions supported. But if we did: String version = (String) getRequest().getAttributes().get("version"); if ( version == null ) { version = LayerConfiguration.DEFAULT_VERSION; } // decode and build JSONObject from request parameters JSONObject decodedQueryParams = QueryParamDecoder.decode( getRequest().getResourceRef().getQuery() ); return new JsonRepresentation( _service.getTranslation(decodedQueryParams) ); } catch ( Exception e) { throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Unable to translate text. Check parameters.", e); } } }
public Token requestToken(OAuthParameters parameters) throws OAuthException, IOException, JSONException { if (authenticationScheme == null) { // Use Body method setupBodyClientCredentials(parameters); } else { setChallengeResponse(authenticationScheme, clientId, clientSecret); } Representation input = parameters.toRepresentation(); getClientInfo().getAcceptedMediaTypes().add( new Preference<MediaType>(MediaType.APPLICATION_JSON)); JSONObject result = new JsonRepresentation(post(input)).getJsonObject(); if (result.has(ERROR)) { throw OAuthException.toOAuthException(result); } TokenResponse token = TokenResponse.parseResponse(result); if (token.scope == null) { // Should be identical to the scope requested by the client. token.scope = Scopes.parseScope(parameters.toForm().getFirstValue( SCOPE)); } return token; }
@Override public Representation get() throws ResourceException { try { String query = getQuery().getFirstValue("query"); String contextClassifierQualifiedName = getQuery().getFirstValue("contextClassifierQualifiedName"); if (contextClassifierQualifiedName != null) { Classifier contextClassifier = (Classifier) ModelLoader.INSTANCE.findNamedElement(contextClassifierQualifiedName); UmlgOcl2Parser.INSTANCE.getHelper().setContext(contextClassifier); } List<Choice> insights = UmlgOcl2Parser.INSTANCE.getCodeInsights(ConstraintKind.INVARIANT, query); return new JsonRepresentation(convertChoicesToJson(insights)); } finally { UMLG.get().rollback(); } }
@SuppressWarnings("unchecked") @ApiOperation(value = "List datastreams in an object", tags = "datastream") @ApiResponses({ @ApiResponse(code = 200, message = "The datastreams are listed"), @ApiResponse(code = 500, message = "Oid does not exist in storage", response = StorageException.class) }) @Get("json") public JsonRepresentation getDatastreamList() throws StorageException, IOException { Storage storage = (Storage) ApplicationContextProvider.getApplicationContext().getBean("fascinatorStorage"); String oid = getAttribute("oid"); DigitalObject digitalObject = StorageUtils.getDigitalObject(storage, oid); JsonObject responseObject = getSuccessResponse(oid); JSONArray dataStreamIds = new JSONArray(); dataStreamIds.addAll(digitalObject.getPayloadIdList()); responseObject.put("datastreamIds", dataStreamIds); return new JsonRepresentation(new JsonSimple(responseObject).toString(true)); }
@Override public Representation get() throws ResourceException { try { String query = getQuery().getFirstValue("query"); String contextClassifierQualifiedName = getQuery().getFirstValue("contextClassifierQualifiedName"); if (contextClassifierQualifiedName != null) { Classifier contextClassifier = (Classifier) ModelLoader.INSTANCE.findNamedElement(contextClassifierQualifiedName); UmlgOcl2Parser.INSTANCE.getHelper().setContext(contextClassifier); } List<Choice> insights = UmlgOcl2Parser.INSTANCE.getHelper().getSyntaxHelp(null, query); return new JsonRepresentation(convertChoicesToJson(insights)); } finally { UMLG.get().rollback(); } }