All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.dynamicextensionsalfresco.webscripts.arguments.JsonObjectArgumentResolver Maven / Gradle / Ivy

Go to download

Adds an OSGi container to alfresco repository supporting dynamic code reloading, classpath isolation and a bunch of other useful features

There is a newer version: 3.1.0
Show newest version
package com.github.dynamicextensionsalfresco.webscripts.arguments;

import org.alfresco.repo.content.MimetypeMap;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptException;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse;

import java.io.IOException;

/**
 * Allows adding optional JSONObject parameters to Uri handler methods.
 *
 * @author Laurent Van der Linden
 */
public class JsonObjectArgumentResolver extends AbstractTypeBasedArgumentResolver {
    @Override
    protected Class getExpectedArgumentType() {
        return JSONObject.class;
    }

    @Override
    protected JSONObject resolveArgument(WebScriptRequest request, WebScriptResponse response) {
        if (request.getContentType().startsWith(MimetypeMap.MIMETYPE_JSON)) {
            try {
                final String contentText = request.getContent().getContent();
                if (contentText != null) {
                    try {
                        return new JSONObject(contentText);
                    } catch (JSONException e) {
                        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Failed to parse JSON body: " + contentText, e);
                    }
                }
            } catch (IOException ex) {
                throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Failed to read body content", ex);
            }
        }
        return null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy