Example usage for org.apache.http.protocol HTTP CONTENT_TYPE

List of usage examples for org.apache.http.protocol HTTP CONTENT_TYPE

Introduction

In this page you can find the example usage for org.apache.http.protocol HTTP CONTENT_TYPE.

Prototype

String CONTENT_TYPE

To view the source code for org.apache.http.protocol HTTP CONTENT_TYPE.

Click Source Link

Usage

From source file:com.hp.mqm.client.MqmRestClientImpl.java

private JSONObject updateEntities(URI uri, String entityJson) {
    HttpPut request = new HttpPut(uri);
    request.setHeader(HTTP.CONTENT_TYPE, "application/json");
    request.setHeader("Accept", "application/json");
    request.setEntity(new StringEntity(entityJson, ContentType.APPLICATION_JSON));
    HttpResponse response;/*from   w  ww  . j  a va 2 s.  c o m*/
    try {
        response = execute(request);
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode == HttpStatus.SC_SERVICE_UNAVAILABLE) {
            throw new TemporarilyUnavailableException("Service not available");
        }
        if (statusCode != HttpStatus.SC_OK) {
            throw createRequestException("Put failed", response);
        }
        String json = IOUtils.toString(response.getEntity().getContent());
        return JSONObject.fromObject(json);
    } catch (IOException e) {
        throw new RequestErrorException("Cannot put entities to MQM.", e);
    }
}

From source file:com.microsoft.windowsazure.mobileservices.MobileServiceClient.java

/**
 * Invokes a custom API//from   w w w .  j  av a 2  s.  co m
 *
 * @param apiName    The API name
 * @param body       The json element to send as the request body
 * @param httpMethod The HTTP Method used to invoke the API
 * @param parameters The query string parameters sent in the request
 * @param features   The features used in the request
 */
private ListenableFuture<JsonElement> invokeApiInternal(String apiName, JsonElement body, String httpMethod,
        List<Pair<String, String>> parameters, EnumSet<MobileServiceFeatures> features) {

    byte[] content = null;
    if (body != null) {
        try {
            content = body.toString().getBytes(UTF8_ENCODING);
        } catch (UnsupportedEncodingException e) {
            throw new IllegalArgumentException(e);
        }
    }

    List<Pair<String, String>> requestHeaders = new ArrayList<Pair<String, String>>();
    if (body != null) {
        requestHeaders
                .add(new Pair<String, String>(HTTP.CONTENT_TYPE, MobileServiceConnection.JSON_CONTENTTYPE));
    }

    if (parameters != null && !parameters.isEmpty()) {
        features.add(MobileServiceFeatures.AdditionalQueryParameters);
    }

    final SettableFuture<JsonElement> future = SettableFuture.create();
    ListenableFuture<ServiceFilterResponse> internalFuture = invokeApiInternal(apiName, content, httpMethod,
            requestHeaders, parameters, features);

    Futures.addCallback(internalFuture, new FutureCallback<ServiceFilterResponse>() {
        @Override
        public void onFailure(Throwable e) {
            future.setException(e);
        }

        @Override
        public void onSuccess(ServiceFilterResponse response) {

            String content = response.getContent();

            if (content == null) {
                future.set(null);

                return;
            }

            JsonElement json = new JsonParser().parse(content);
            future.set(json);
        }
    });

    return future;
}

From source file:org.apache.synapse.debug.SynapseDebugManager.java

public void addMediationFlowPointProperty(String propertyContext, JSONObject property_arguments,
        boolean isActionSet) {
    try {//  w ww.ja va 2s . co m
        String propertyKey = property_arguments
                .getString(SynapseDebugCommandConstants.DEBUG_COMMAND_PROPERTY_NAME);
        if (isActionSet) {
            String propertyValue = property_arguments
                    .getString(SynapseDebugCommandConstants.DEBUG_COMMAND_PROPERTY_VALUE);
            if (propertyContext.equals(SynapseDebugCommandConstants.DEBUG_COMMAND_PROPERTY_CONTEXT_DEFAULT)
                    || propertyContext
                            .equals(SynapseDebugCommandConstants.DEBUG_COMMAND_PROPERTY_CONTEXT_SYNAPSE)) {
                synCtx.setProperty(propertyKey, propertyValue);

            } else if (propertyContext.equals(SynapseDebugCommandConstants.DEBUG_COMMAND_PROPERTY_CONTEXT_AXIS2)
                    && synCtx instanceof Axis2MessageContext) {
                Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
                org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
                setAxis2Property(propertyKey, propertyValue, axis2MessageCtx);
                if (org.apache.axis2.Constants.Configuration.MESSAGE_TYPE.equalsIgnoreCase(propertyKey)) {
                    setAxis2Property(org.apache.axis2.Constants.Configuration.CONTENT_TYPE, propertyValue,
                            axis2MessageCtx);
                    Object o = axis2MessageCtx
                            .getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
                    Map headers = (Map) o;
                    if (headers != null) {
                        headers.remove(HTTP.CONTENT_TYPE);
                        headers.put(HTTP.CONTENT_TYPE, propertyValue);
                    }
                }
            } else if (propertyContext
                    .equals(SynapseDebugCommandConstants.DEBUG_COMMAND_PROPERTY_CONTEXT_AXIS2CLIENT)
                    && synCtx instanceof Axis2MessageContext) {
                Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
                org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
                axis2MessageCtx.getOptions().setProperty(propertyKey, propertyValue);
            } else if (propertyContext
                    .equals(SynapseDebugCommandConstants.DEBUG_COMMAND_PROPERTY_CONTEXT_TRANSPORT)
                    && synCtx instanceof Axis2MessageContext) {
                Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
                org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
                Object headers = axis2MessageCtx
                        .getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
                if (headers != null && headers instanceof Map) {
                    Map headersMap = (Map) headers;
                    headersMap.put(propertyKey, propertyValue);
                }
                if (headers == null) {
                    Map headersMap = new HashMap();
                    headersMap.put(propertyKey, propertyValue);
                    axis2MessageCtx.setProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS,
                            headersMap);
                }
            } else if (propertyContext
                    .equals(SynapseDebugCommandConstants.DEBUG_COMMAND_PROPERTY_CONTEXT_OPERATION)
                    && synCtx instanceof Axis2MessageContext) {
                Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
                axis2smc.getAxis2MessageContext().getOperationContext().setProperty(propertyKey, propertyValue);
            }
        } else {
            if (propertyContext == null
                    || SynapseDebugCommandConstants.DEBUG_COMMAND_PROPERTY_CONTEXT_DEFAULT
                            .equals(propertyContext)
                    || SynapseDebugCommandConstants.DEBUG_COMMAND_PROPERTY_CONTEXT_SYNAPSE
                            .equals(propertyContext)) {
                Set pros = synCtx.getPropertyKeySet();
                if (pros != null) {
                    pros.remove(propertyKey);
                }
            } else if (propertyContext.equals(SynapseDebugCommandConstants.DEBUG_COMMAND_PROPERTY_CONTEXT_AXIS2)
                    && synCtx instanceof Axis2MessageContext) {
                Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
                org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
                axis2MessageCtx.removeProperty(propertyKey);
            } else if (propertyContext
                    .equals(SynapseDebugCommandConstants.DEBUG_COMMAND_PROPERTY_CONTEXT_AXIS2CLIENT)
                    && synCtx instanceof Axis2MessageContext) {
                Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
                org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
                axis2MessageCtx.getOptions().setProperty(propertyKey, EMPTY_STRING);
            } else if (propertyContext
                    .equals(SynapseDebugCommandConstants.DEBUG_COMMAND_PROPERTY_CONTEXT_TRANSPORT)
                    && synCtx instanceof Axis2MessageContext) {
                Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
                org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
                Object headers = axis2MessageCtx
                        .getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
                if (headers != null && headers instanceof Map) {
                    Map headersMap = (Map) headers;
                    headersMap.remove(propertyKey);
                }
            } else if (propertyContext
                    .equals(SynapseDebugCommandConstants.DEBUG_COMMAND_PROPERTY_CONTEXT_OPERATION)
                    && synCtx instanceof Axis2MessageContext) {
                Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
                axis2smc.getAxis2MessageContext().getOperationContext().removeProperty(propertyKey);
            } else {
                log.error("Failed to set or remove property in the scope " + propertyContext);
                this.advertiseCommandResponse(createDebugCommandResponse(false,
                        SynapseDebugCommandConstants.DEBUG_COMMAND_RESPONSE_UNABLE_TO_ALTER_MESSAGE_CONTEXT_PROPERTY)
                                .toString());
            }
        }
    } catch (JSONException e) {
        log.error("Failed to set or remove property in the scope " + propertyContext, e);
        this.advertiseCommandResponse(createDebugCommandResponse(false,
                SynapseDebugCommandConstants.DEBUG_COMMAND_RESPONSE_UNABLE_TO_ALTER_MESSAGE_CONTEXT_PROPERTY)
                        .toString());
    }
    this.advertiseCommandResponse(createDebugCommandResponse(true, null).toString());
}

From source file:org.apache.synapse.SynapseDebugManager.java

public void addMediationFlowPointProperty(String propertyContext, JSONObject property_arguments,
        boolean isActionSet) {
    try {/*from w  ww . j  av a 2 s.  c  om*/
        String property_key = property_arguments
                .getString(SynapseDebugCommandConstants.DEBUG_COMMAND_PROPERTY_NAME);
        String property_value = property_arguments
                .getString(SynapseDebugCommandConstants.DEBUG_COMMAND_PROPERTY_VALUE);//bug
        if (isActionSet) {

            if (propertyContext.equals(SynapseDebugCommandConstants.DEBUG_COMMAND_PROPERTY_CONTEXT_DEFAULT)
                    || propertyContext
                            .equals(SynapseDebugCommandConstants.DEBUG_COMMAND_PROPERTY_CONTEXT_SYNAPSE)) {
                //Setting property into the  Synapse Context
                synCtx.setProperty(property_key, property_value);

            } else if (propertyContext.equals(SynapseDebugCommandConstants.DEBUG_COMMAND_PROPERTY_CONTEXT_AXIS2)
                    && synCtx instanceof Axis2MessageContext) {
                //Setting property into the  Axis2 Message Context
                Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
                org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
                axis2MessageCtx.setProperty(property_key, property_value);
                if (org.apache.axis2.Constants.Configuration.MESSAGE_TYPE.equals(property_key)) {
                    axis2MessageCtx.setProperty(org.apache.axis2.Constants.Configuration.CONTENT_TYPE,
                            property_value);
                    Object o = axis2MessageCtx
                            .getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
                    Map _headers = (Map) o;
                    if (_headers != null) {
                        _headers.remove(HTTP.CONTENT_TYPE);
                        _headers.put(HTTP.CONTENT_TYPE, property_value);
                    }
                }

            } else if (propertyContext
                    .equals(SynapseDebugCommandConstants.DEBUG_COMMAND_PROPERTY_CONTEXT_AXIS2CLIENT)
                    && synCtx instanceof Axis2MessageContext) {
                //Setting property into the  Axis2 Message Context client options
                Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
                org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
                axis2MessageCtx.getOptions().setProperty(property_key, property_value);

            } else if (propertyContext
                    .equals(SynapseDebugCommandConstants.DEBUG_COMMAND_PROPERTY_CONTEXT_TRANSPORT)
                    && synCtx instanceof Axis2MessageContext) {
                //Setting Transport Headers
                Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
                org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
                Object headers = axis2MessageCtx
                        .getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);

                if (headers != null && headers instanceof Map) {
                    Map headersMap = (Map) headers;
                    headersMap.put(property_key, property_value);
                }
                if (headers == null) {
                    Map headersMap = new HashMap();
                    headersMap.put(property_key, property_value);
                    axis2MessageCtx.setProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS,
                            headersMap);
                }
            } else if (propertyContext
                    .equals(SynapseDebugCommandConstants.DEBUG_COMMAND_PROPERTY_CONTEXT_OPERATION)
                    && synCtx instanceof Axis2MessageContext) {
                //Setting Transport Headers
                Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
                org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
                axis2smc.getAxis2MessageContext().getOperationContext().setProperty(property_key,
                        property_value);
            }

        } else {
            if (propertyContext == null || XMLConfigConstants.SCOPE_DEFAULT.equals(propertyContext)) {
                //Removing property from the  Synapse Context
                Set pros = synCtx.getPropertyKeySet();
                if (pros != null) {
                    pros.remove(property_key);
                }

            } else if ((propertyContext
                    .equals(SynapseDebugCommandConstants.DEBUG_COMMAND_PROPERTY_CONTEXT_AXIS2)
                    || propertyContext
                            .equals(SynapseDebugCommandConstants.DEBUG_COMMAND_PROPERTY_CONTEXT_AXIS2CLIENT))
                    && synCtx instanceof Axis2MessageContext) {

                //Removing property from the Axis2 Message Context
                Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
                org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
                axis2MessageCtx.removeProperty(property_key);

            } else if (propertyContext
                    .equals(SynapseDebugCommandConstants.DEBUG_COMMAND_PROPERTY_CONTEXT_TRANSPORT)
                    && synCtx instanceof Axis2MessageContext) {
                // Removing transport headers
                Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
                org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
                Object headers = axis2MessageCtx
                        .getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
                if (headers != null && headers instanceof Map) {
                    Map headersMap = (Map) headers;
                    headersMap.remove(property_key);
                } else {

                }
            }
        }

    } catch (JSONException e) {
        log.error("Failed to set or remove property in the scope: " + propertyContext, e);
    }
    try {
        this.advertiseCommandResponse(createDebugCommandResponse(true, null).toString()); // need error handle
    } catch (JSONException e) {
        log.error("Unable to advertise command response", e);
    }
}

From source file:com.barion.example.app2app.nativeintegration.fragments.ShopFragment.java

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.payButton:
        if (getActivity() != null) {
            progressDialog = ProgressDialog.show(getActivity(), null, getString(R.string.preparingpayment),
                    true, false);//  w w w. j a va 2 s. co  m
            new AsyncTask<Void, Void, String>() {
                @Override
                protected String doInBackground(Void... params) {
                    HttpClient httpClient = new DefaultHttpClient();
                    HttpPost post = new HttpPost(Preferences.URL_STARTPAYMENT);
                    try {

                        JSONArray productsArray = new JSONArray();
                        for (int i = 0; i < products.size(); ++i) {
                            Product p = products.get(i);
                            JSONObject productJson = new JSONObject();
                            productJson.put("Name", p.getName()).put("Description", p.getDescription())
                                    .put("Quantity", p.getQuantity()).put("Unit", p.getUnit())
                                    .put("UnitPrice", p.getPrice()).put("ItemTotal", p.getPrice())
                                    .put("SKU", p.getSKU());
                            productsArray.put(productJson);
                        }

                        JSONObject holderJson = new JSONObject();
                        holderJson.put("Products", productsArray);

                        post.setHeader(HTTP.CONTENT_TYPE, "application/json;charset=UTF-8");
                        post.setEntity(new StringEntity(holderJson.toString(), "UTF-8"));

                        HttpResponse response = httpClient.execute(post);
                        InputStream stream = response.getEntity().getContent();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
                        StringBuilder builder = new StringBuilder();

                        String line;

                        while ((line = reader.readLine()) != null) {
                            builder.append(line);
                        }

                        return builder.toString();
                    } catch (Exception x) {
                        return null;
                    }
                }

                @Override
                protected void onPostExecute(String s) {
                    if (s == null)
                        networkError();
                    try {
                        handler.sendEmptyMessage(0);
                        JSONObject resultObject = new JSONObject(s);
                        BarionStartPaymentResponse response = BarionStartPaymentResponse
                                .fromJsonObject(resultObject);
                        if (response.getErrors().size() != 0)
                            processBarionErrors(response.getErrors());
                        else
                            navigateUserToBarion(response);
                    } catch (JSONException e) {
                        networkError();
                    }

                }
            }.execute();
        }
        break;
    }
}

From source file:com.newrelic.agent.android.harvest.HarvestConnection.java

public HttpPost createPost(String str, String str2) {
    String str3 = (str2.length() <= AccessibilityNodeInfoCompat.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY
            || DISABLE_COMPRESSION_FOR_DEBUGGING.booleanValue()) ? HTTP.IDENTITY_CODING : "deflate";
    HttpPost httpPost = new HttpPost(str);
    httpPost.addHeader(HTTP.CONTENT_TYPE, AbstractSpiCall.ACCEPT_JSON_VALUE);
    httpPost.addHeader(HTTP.CONTENT_ENCODING, str3);
    httpPost.addHeader(HTTP.USER_AGENT, System.getProperty("http.agent"));
    if (this.applicationToken == null) {
        this.log.error("Cannot create POST without an Application Token.");
        return null;
    }/*from  w  w w .j  av  a  2  s  .  c o  m*/
    httpPost.addHeader(APPLICATION_TOKEN_HEADER, this.applicationToken);
    if (this.serverTimestamp != 0) {
        httpPost.addHeader(CONNECT_TIME_HEADER, Long.valueOf(this.serverTimestamp).toString());
    }
    if ("deflate".equals(str3)) {
        httpPost.setEntity(new ByteArrayEntity(deflate(str2)));
    } else {
        try {
            httpPost.setEntity(new StringEntity(str2, "utf-8"));
        } catch (Throwable e) {
            this.log.error("UTF-8 is unsupported");
            throw new IllegalArgumentException(e);
        }
    }
    return httpPost;
}

From source file:org.alfresco.dataprep.AlfrescoHttpClient.java

/**
 * Populate HTTP message call with given content.
 * @param json {@link JSONObject} content
 * @return {@link StringEntity} content.
 */// ww w. j a  v  a  2 s. co m
public StringEntity setMessageBody(final JSONObject json) {
    if (json == null || json.toString().isEmpty()) {
        throw new IllegalArgumentException("JSON Content is required.");
    }
    StringEntity se = new StringEntity(json.toString(), UTF_8_ENCODING);
    se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, MIME_TYPE_JSON));
    if (logger.isDebugEnabled()) {
        logger.debug("Json string value: " + se);
    }
    return se;
}

From source file:org.alfresco.dataprep.ContentService.java

/**
 * Update content of a document/*  w  w w  .  ja  va  2s . c om*/
 * @param userName login username
 * @param password login password
 * @param siteName site name
 * @param docType file type
 * @param docName file name to be updated
 * @param newContent new content of the file
 * @return true if updated
 */
private boolean updateDocumentContent(final String userName, final String password, final boolean byPath,
        final String pathToDocument, final String siteName, final String docName, final DocumentType docType,
        final String newContent) {
    AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject();
    String docNodeRef;
    if (byPath) {
        docNodeRef = getNodeRefByPath(userName, password, pathToDocument);
    } else {
        docNodeRef = getNodeRef(userName, password, siteName, docName);
    }
    if (StringUtils.isEmpty(docNodeRef)) {
        throw new RuntimeException("Content doesn't exists");
    }
    String serviceUrl = client.getApiUrl().replace("service/", "")
            + "-default-/public/cmis/versions/1.1/atom/content?id=" + docNodeRef;
    HttpPut request = new HttpPut(serviceUrl);
    String contentType = docType.type + ";charset=" + AlfrescoHttpClient.UTF_8_ENCODING;
    request.addHeader("Content-Type", contentType);
    StringEntity se = new StringEntity(newContent.toString(), AlfrescoHttpClient.UTF_8_ENCODING);
    se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, AlfrescoHttpClient.MIME_TYPE_JSON));
    request.setEntity(se);
    waitInSeconds(1);
    try {
        HttpResponse response = client.executeAndRelease(userName, password, request);
        if (HttpStatus.SC_CREATED == response.getStatusLine().getStatusCode()) {
            return true;
        }
    } finally {
        request.releaseConnection();
        client.close();
    }
    return false;
}