Search in sources :

Example 1 with JsonProcessingException

use of org.codehaus.jackson.JsonProcessingException in project OpenAttestation by OpenAttestation.

the class AbstractSimpleResource method deleteOne.

//    /**
//     * Add an item to the collection. Input Content-Type is any of
//     * application/json, application/xml, application/yaml, or text/yaml Output
//     * Content-Type is any of application/json, application/xml,
//     * application/yaml, or text/yaml
//     *
//     * The input must represent a single item NOT wrapped in a collection.
//     *
//     * @param item
//     * @return
//     */
//    @POST
//    public T createOne(@BeanParam L locator, T item, @Context HttpServletRequest httpServletRequest, @Context HttpServletResponse httpServletResponse){
//        //try { log.debug("createOne: {}", mapper.writeValueAsString(locator)); } catch(JsonProcessingException e) { log.debug("createOne: cannot serialize locator: {}", e.getMessage()); }
//        try { log.debug("createOne: {}", mapper.writeValueAsString(locator)); } catch(Exception e) { log.debug("createOne: cannot serialize locator: {}", e.getMessage()); }
//        locator.copyTo(item);
//        ValidationUtil.validate(item); // throw new MWException(e, ErrorCode.AS_INPUT_VALIDATION_ERROR, input, method.getName());
//        if (item.getId() == null) {
//            item.setId(new UUID());
//        }
//        getRepository().create(item);
//        httpServletResponse.setStatus(Status.CREATED.getStatusCode());
//        return item;
//    }
// the delete method is on a specific resource id and because we don't return any content it's the same whether its simple object or json api 
// jersey automatically returns status code 204 No Content (successful) to the client because
// we have a void return type
@Path("/{id}")
@DELETE
public void deleteOne(@BeanParam L locator, @Context HttpServletRequest httpServletRequest, @Context HttpServletResponse httpServletResponse) throws IOException {
    try {
        log.debug("deleteOne: {}", mapper.writeValueAsString(locator));
    } catch (JsonProcessingException e) {
        log.debug("deleteOne: cannot serialize locator: {}", e.getMessage());
    }
    // subclass is responsible for validating the id in whatever manner it needs to;  most will return null if !UUID.isValid(id)  but we don't do it here because a resource might want to allow using something other than uuid as the url key, for example uuid OR hostname for hosts
    T item = getRepository().retrieve(locator);
    if (item == null) {
        throw new WebApplicationException(Response.Status.NOT_FOUND);
    }
    getRepository().delete(locator);
    httpServletResponse.setStatus(Status.NO_CONTENT.getStatusCode());
/*
        T item = getRepository().retrieve(id); // subclass is responsible for validating the id in whatever manner it needs to;  most will return null if !UUID.isValid(id)  but we don't do it here because a resource might want to allow using something other than uuid as the url key, for example uuid OR hostname for hosts
        if (item == null) {
            throw new WebApplicationException(Response.Status.NOT_FOUND); 
        }
        getRepository().delete(id);*/
/*
//        C collection = getRepository().search(selector);
//        if( collection.getDocuments().isEmpty() ) {            
//            throw new WebApplicationException(Response.Status.NOT_FOUND); 
//        }
//        T item = collection.getDocuments().get(0);
        
//        getRepository().delete(item.getId().toString());
* */
}
Also used : GET(javax.ws.rs.GET) POST(javax.ws.rs.POST) PUT(javax.ws.rs.PUT) WebApplicationException(javax.ws.rs.WebApplicationException) JsonProcessingException(org.codehaus.jackson.JsonProcessingException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Example 2 with JsonProcessingException

use of org.codehaus.jackson.JsonProcessingException in project openhab1-addons by openhab.

the class MyqData method request.

/**
     * Make a request to the server, optionally retry the call if there is a
     * login issue. Will throw a InvalidLoginExcpetion if the account is
     * invalid, locked or soon to be locked.
     *
     * @param method
     *            The Http Method Type (GET,PUT)
     * @param url
     *            The request URL
     * @param payload
     *            Payload string for put operations
     * @param payloadType
     *            Payload content type for put operations
     * @param retry
     *            Retry the attempt if our session key is not valid
     * @return The JsonNode representing the response data
     * @throws IOException
     * @throws InvalidLoginException
     */
private synchronized JsonNode request(String method, String url, String payload, String payloadType, boolean retry) throws IOException, InvalidLoginException {
    logger.trace("Requesting URL {}", url);
    String dataString = executeUrl(method, url, header, payload == null ? null : IOUtils.toInputStream(payload), payloadType, timeout);
    logger.trace("Received MyQ JSON: {}", dataString);
    if (dataString == null) {
        throw new IOException("Null response from MyQ server");
    }
    try {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode rootNode = mapper.readTree(dataString);
        int returnCode = rootNode.get("ReturnCode").asInt();
        logger.trace("myq ReturnCode: {}", returnCode);
        MyQResponseCode rc = MyQResponseCode.fromCode(returnCode);
        switch(rc) {
            case OK:
                {
                    return rootNode;
                }
            case ACCOUNT_INVALID:
            case ACCOUNT_NOT_FOUND:
            case ACCOUNT_LOCKED:
            case ACCOUNT_LOCKED_PENDING:
                // lock an account
                throw new InvalidLoginException(rc.getDesc());
            case LOGIN_ERROR:
                // Our session key has expired, request a new one
                if (retry) {
                    login();
                    return request(method, url, payload, payloadType, false);
                }
            // fall through to default
            default:
                throw new IOException("Request Failed: " + rc.getDesc());
        }
    } catch (JsonProcessingException e) {
        throw new IOException("Could not parse response", e);
    }
}
Also used : JsonNode(org.codehaus.jackson.JsonNode) IOException(java.io.IOException) JsonProcessingException(org.codehaus.jackson.JsonProcessingException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 3 with JsonProcessingException

use of org.codehaus.jackson.JsonProcessingException in project perun by CESNET.

the class JsonSerializer method write.

@Override
public void write(Object object) throws RpcException, IOException {
    JsonGenerator gen = jsonFactory.createJsonGenerator(out, JsonEncoding.UTF8);
    try {
        gen.writeObject(object);
        gen.flush();
        gen.close();
    } catch (JsonProcessingException ex) {
        throw new RpcException(RpcException.Type.CANNOT_SERIALIZE_VALUE, ex);
    }
}
Also used : RpcException(cz.metacentrum.perun.core.api.exceptions.RpcException) JsonGenerator(org.codehaus.jackson.JsonGenerator) JsonProcessingException(org.codehaus.jackson.JsonProcessingException)

Example 4 with JsonProcessingException

use of org.codehaus.jackson.JsonProcessingException in project perun by CESNET.

the class JsonSerializer method write.

@Override
public void write(Object object) throws RpcException, IOException {
    JsonGenerator gen = jsonFactory.createJsonGenerator(out, JsonEncoding.UTF8);
    if (object instanceof Throwable) {
        throw new IllegalArgumentException("Tried to serialize a throwable object using write()", (Throwable) object);
    }
    try {
        gen.writeObject(object);
        gen.close();
    } catch (JsonProcessingException ex) {
        throw new RpcException(RpcException.Type.CANNOT_SERIALIZE_VALUE, ex);
    }
}
Also used : RpcException(cz.metacentrum.perun.core.api.exceptions.RpcException) JsonGenerator(org.codehaus.jackson.JsonGenerator) JsonProcessingException(org.codehaus.jackson.JsonProcessingException)

Example 5 with JsonProcessingException

use of org.codehaus.jackson.JsonProcessingException in project perun by CESNET.

the class JsonSerializerJSONP method write.

@Override
public void write(Object object) throws RpcException, IOException {
    JsonGenerator gen = jsonFactory.createJsonGenerator(out, JsonEncoding.UTF8);
    if (object instanceof Throwable) {
        throw new IllegalArgumentException("Tried to serialize a throwable object using write()", (Throwable) object);
    }
    try {
        gen.writeRaw(callback + "(");
        gen.writeObject(object);
        gen.writeRaw(");");
        gen.flush();
        gen.close();
    } catch (JsonProcessingException ex) {
        throw new RpcException(RpcException.Type.CANNOT_SERIALIZE_VALUE, ex);
    }
}
Also used : RpcException(cz.metacentrum.perun.core.api.exceptions.RpcException) JsonGenerator(org.codehaus.jackson.JsonGenerator) JsonProcessingException(org.codehaus.jackson.JsonProcessingException)

Aggregations

JsonProcessingException (org.codehaus.jackson.JsonProcessingException)10 JsonGenerator (org.codehaus.jackson.JsonGenerator)6 RpcException (cz.metacentrum.perun.core.api.exceptions.RpcException)5 IOException (java.io.IOException)4 JsonNode (org.codehaus.jackson.JsonNode)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Version (org.codehaus.jackson.Version)2 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)2 SimpleModule (org.codehaus.jackson.map.module.SimpleModule)2 StringCodec (com.datatorrent.api.StringCodec)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 DELETE (javax.ws.rs.DELETE)1 GET (javax.ws.rs.GET)1 POST (javax.ws.rs.POST)1 PUT (javax.ws.rs.PUT)1 Path (javax.ws.rs.Path)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 BeanMap (org.apache.commons.beanutils.BeanMap)1