Search in sources :

Example 56 with JsonFactory

use of com.fasterxml.jackson.core.JsonFactory in project cassandra-mesos-deprecated by mesosphere.

the class AbstractApiControllerTest method fetchJson.

@NotNull
protected Tuple2<Integer, JsonNode> fetchJson(final String rel, final boolean post) throws Exception {
    final JsonFactory factory = new JsonFactory();
    final HttpURLConnection conn = (HttpURLConnection) resolve(rel).toURL().openConnection();
    try {
        conn.setRequestMethod(post ? "POST" : "GET");
        conn.setRequestProperty("Accept", "application/json");
        conn.connect();
        final int responseCode = conn.getResponseCode();
        InputStream in;
        try {
            in = conn.getInputStream();
        } catch (final IOException e) {
            in = conn.getErrorStream();
        }
        if (in == null) {
            return Tuple2.tuple2(responseCode, (JsonNode) MissingNode.getInstance());
        }
        assertEquals("application/json", conn.getHeaderField("Content-Type"));
        try {
            final ObjectMapper om = new ObjectMapper();
            return Tuple2.tuple2(responseCode, om.reader().with(factory).readTree(in));
        } finally {
            in.close();
        }
    } finally {
        conn.disconnect();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStream(java.io.InputStream) JsonFactory(com.fasterxml.jackson.core.JsonFactory) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) NotNull(org.jetbrains.annotations.NotNull)

Example 57 with JsonFactory

use of com.fasterxml.jackson.core.JsonFactory in project android-async-http by loopj.

the class Http401AuthSample method getResponseHandler.

@Override
public ResponseHandlerInterface getResponseHandler() {
    return new BaseJsonHttpResponseHandler<SampleJSON>() {

        @Override
        public void onStart() {
            clearOutputs();
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, String rawJsonResponse, SampleJSON response) {
            debugHeaders(LOG_TAG, headers);
            debugStatusCode(LOG_TAG, statusCode);
            if (response != null) {
                debugResponse(LOG_TAG, rawJsonResponse);
            }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, Throwable throwable, String rawJsonData, SampleJSON errorResponse) {
            debugHeaders(LOG_TAG, headers);
            debugStatusCode(LOG_TAG, statusCode);
            debugThrowable(LOG_TAG, throwable);
            // Ask the user for credentials if required by the server.
            if (statusCode == 401) {
                String realm = "Protected Page";
                String authType = null;
                // Cycle through the headers and look for the WWW-Authenticate header.
                for (Header header : headers) {
                    String headerName = header.getName();
                    if (HEADER_WWW_AUTHENTICATE.equalsIgnoreCase(headerName)) {
                        String headerValue = header.getValue().trim();
                        String headerValueLowerCase = headerValue.toLowerCase(Locale.US);
                        // Get the type of auth requested.
                        int charPos = headerValueLowerCase.indexOf(' ');
                        if (0 < charPos) {
                            authType = headerValueLowerCase.substring(0, charPos);
                            // The second part should begin with a "realm=" prefix.
                            if (headerValueLowerCase.substring(1 + charPos).startsWith(HEADER_REALM_PREFIX)) {
                                // The new realm value, including any possible wrapping quotation.
                                realm = headerValue.substring(1 + charPos + HEADER_REALM_PREFIX.length());
                                // If realm starts with a quote, remove surrounding quotes.
                                if (realm.charAt(0) == '"' || realm.charAt(0) == '\'') {
                                    realm = realm.substring(1, realm.length() - 1);
                                }
                            }
                        }
                    }
                }
                // We will support basic auth in this sample.
                if (authType != null && HEADER_BASIC.equals(authType)) {
                    // Show a dialog for the user and request user/pass.
                    Log.d(LOG_TAG, HEADER_REALM_PREFIX + realm);
                    // Present the dialog.
                    postRunnable(new DialogRunnable(realm));
                }
            }
        }

        @Override
        protected SampleJSON parseResponse(String rawJsonData, boolean isFailure) throws Throwable {
            return new ObjectMapper().readValues(new JsonFactory().createParser(rawJsonData), SampleJSON.class).next();
        }
    };
}
Also used : Header(cz.msebera.android.httpclient.Header) BasicHeader(cz.msebera.android.httpclient.message.BasicHeader) SampleJSON(com.loopj.android.http.sample.util.SampleJSON) JsonFactory(com.fasterxml.jackson.core.JsonFactory) BaseJsonHttpResponseHandler(com.loopj.android.http.BaseJsonHttpResponseHandler) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 58 with JsonFactory

use of com.fasterxml.jackson.core.JsonFactory in project android-async-http by loopj.

the class JsonSample method getResponseHandler.

@Override
public ResponseHandlerInterface getResponseHandler() {
    return new BaseJsonHttpResponseHandler<SampleJSON>() {

        @Override
        public void onStart() {
            clearOutputs();
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, String rawJsonResponse, SampleJSON response) {
            debugHeaders(LOG_TAG, headers);
            debugStatusCode(LOG_TAG, statusCode);
            if (response != null) {
                debugResponse(LOG_TAG, rawJsonResponse);
            }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, Throwable throwable, String rawJsonData, SampleJSON errorResponse) {
            debugHeaders(LOG_TAG, headers);
            debugStatusCode(LOG_TAG, statusCode);
            debugThrowable(LOG_TAG, throwable);
            if (errorResponse != null) {
                debugResponse(LOG_TAG, rawJsonData);
            }
        }

        @Override
        protected SampleJSON parseResponse(String rawJsonData, boolean isFailure) throws Throwable {
            return new ObjectMapper().readValues(new JsonFactory().createParser(rawJsonData), SampleJSON.class).next();
        }
    };
}
Also used : SampleJSON(com.loopj.android.http.sample.util.SampleJSON) JsonFactory(com.fasterxml.jackson.core.JsonFactory) BaseJsonHttpResponseHandler(com.loopj.android.http.BaseJsonHttpResponseHandler) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 59 with JsonFactory

use of com.fasterxml.jackson.core.JsonFactory in project android-async-http by loopj.

the class PersistentCookiesSample method getResponseHandler.

@Override
public ResponseHandlerInterface getResponseHandler() {
    return new BaseJsonHttpResponseHandler<SampleJSON>() {

        @Override
        public void onStart() {
            clearOutputs();
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, String rawJsonResponse, SampleJSON response) {
            debugHeaders(LOG_TAG, headers);
            debugStatusCode(LOG_TAG, statusCode);
            if (response != null) {
                debugResponse(LOG_TAG, rawJsonResponse);
            }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, Throwable throwable, String rawJsonData, SampleJSON errorResponse) {
            debugHeaders(LOG_TAG, headers);
            debugStatusCode(LOG_TAG, statusCode);
            debugThrowable(LOG_TAG, throwable);
            if (errorResponse != null) {
                debugResponse(LOG_TAG, rawJsonData);
            }
        }

        @Override
        protected SampleJSON parseResponse(String rawJsonData, boolean isFailure) throws Throwable {
            return new ObjectMapper().readValues(new JsonFactory().createParser(rawJsonData), SampleJSON.class).next();
        }
    };
}
Also used : SampleJSON(com.loopj.android.http.sample.util.SampleJSON) JsonFactory(com.fasterxml.jackson.core.JsonFactory) BaseJsonHttpResponseHandler(com.loopj.android.http.BaseJsonHttpResponseHandler) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 60 with JsonFactory

use of com.fasterxml.jackson.core.JsonFactory in project hadoop by apache.

the class JMXJsonServlet method init.

/**
   * Initialize this servlet.
   */
@Override
public void init() throws ServletException {
    // Retrieve the MBean server
    mBeanServer = ManagementFactory.getPlatformMBeanServer();
    jsonFactory = new JsonFactory();
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory)

Aggregations

JsonFactory (com.fasterxml.jackson.core.JsonFactory)101 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)39 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)33 Test (org.junit.Test)31 StringWriter (java.io.StringWriter)29 JsonParser (com.fasterxml.jackson.core.JsonParser)24 ExtensibleJSONWriter (com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter)15 IOException (java.io.IOException)14 Map (java.util.Map)14 HashMap (java.util.HashMap)11 SimpleParseUUT (com.instagram.common.json.annotation.processor.uut.SimpleParseUUT)8 ArrayList (java.util.ArrayList)8 List (java.util.List)8 FileInputStream (java.io.FileInputStream)6 ServletOutputStream (javax.servlet.ServletOutputStream)6 Reader (java.io.Reader)5 RemoteSession (org.apache.jackrabbit.oak.remote.RemoteSession)5 File (java.io.File)4 JSONWriter (org.json.JSONWriter)4 BaseJsonHttpResponseHandler (com.loopj.android.http.BaseJsonHttpResponseHandler)3