Example usage for org.apache.http.entity ContentType APPLICATION_JSON

List of usage examples for org.apache.http.entity ContentType APPLICATION_JSON

Introduction

In this page you can find the example usage for org.apache.http.entity ContentType APPLICATION_JSON.

Prototype

ContentType APPLICATION_JSON

To view the source code for org.apache.http.entity ContentType APPLICATION_JSON.

Click Source Link

Usage

From source file:com.ibm.watson.app.common.util.rest.JSONEntity.java

private JSONEntity(Object object, Type type) throws UnsupportedEncodingException {
    super(gson.toJson(object, type), ContentType.APPLICATION_JSON);
}

From source file:gertjvr.slacknotifier.SlackApiProcessor.java

public void sendNotification(String url, SlackNotificationMessage notification) throws IOException {
    String content = new Gson().toJson(notification);
    logger.debug(String.format("sendNotification: %s", content));

    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(url);

    httpPost.setEntity(new StringEntity(content, ContentType.APPLICATION_JSON));
    CloseableHttpResponse response = httpClient.execute(httpPost);

    try {/* www.j  av a2s.  co  m*/
        HttpEntity entity = response.getEntity();
        EntityUtils.consume(entity);
    } catch (Exception ex) {
        logger.error(String.format("sendNotification %s", ex.getMessage()), ex);
    } finally {
        response.close();
    }
}

From source file:org.kitodo.data.index.elasticsearch.type.UserGroupType.java

@SuppressWarnings("unchecked")
@Override//w w  w . ja v a 2 s.c  o  m
public HttpEntity createDocument(UserGroup userGroup) {

    LinkedHashMap<String, String> orderedUserGroupMap = new LinkedHashMap<>();
    orderedUserGroupMap.put("title", userGroup.getTitle());
    orderedUserGroupMap.put("permission", userGroup.getPermission().toString());

    JSONArray users = new JSONArray();
    List<User> userGroupUsers = userGroup.getUsers();
    for (User user : userGroupUsers) {
        JSONObject userObject = new JSONObject();
        userObject.put("id", user.getId().toString());
        users.add(userObject);
    }

    JSONObject userGroupObject = new JSONObject(orderedUserGroupMap);
    userGroupObject.put("users", users);

    return new NStringEntity(userGroupObject.toJSONString(), ContentType.APPLICATION_JSON);
}

From source file:org.kitodo.data.elasticsearch.index.type.UserType.java

@SuppressWarnings("unchecked")
@Override/*from   w  w  w .j  a v a2 s.  c  o  m*/
public HttpEntity createDocument(User user) {

    JSONObject userObject = new JSONObject();
    userObject.put("name", user.getName());
    userObject.put("surname", user.getSurname());
    userObject.put("login", user.getLogin());
    userObject.put("ldapLogin", user.getLdapLogin());
    userObject.put("active", String.valueOf(user.isActive()));
    userObject.put("location", user.getLocation());
    userObject.put("metadataLanguage", user.getMetadataLanguage());
    userObject.put("userGroups", addObjectRelation(user.getUserGroups()));
    userObject.put("filters", addObjectRelation(user.getFilters()));
    userObject.put("projects", addObjectRelation(user.getProjects()));
    userObject.put("processingTasks", addObjectRelation(user.getProcessingTasks()));
    userObject.put("tasks", addObjectRelation(user.getTasks()));

    return new NStringEntity(userObject.toJSONString(), ContentType.APPLICATION_JSON);
}

From source file:org.kitodo.data.index.elasticsearch.type.BatchType.java

@SuppressWarnings("unchecked")
@Override/* w  ww  .  j  a  v  a  2s  .  c  o  m*/
public HttpEntity createDocument(Batch batch) {

    LinkedHashMap<String, String> orderedBatchMap = new LinkedHashMap<>();
    orderedBatchMap.put("title", batch.getTitle());
    String type = batch.getType() != null ? batch.getType().toString() : "null";
    orderedBatchMap.put("type", type);

    JSONArray processes = new JSONArray();
    List<Process> batchProcesses = batch.getProcesses();
    for (Process process : batchProcesses) {
        JSONObject processObject = new JSONObject();
        processObject.put("id", process.getId().toString());
        processes.add(processObject);
    }

    JSONObject batchObject = new JSONObject(orderedBatchMap);
    batchObject.put("processes", processes);

    return new NStringEntity(batchObject.toJSONString(), ContentType.APPLICATION_JSON);
}

From source file:com.ibm.ra.remy.web.utils.BusinessRulesUtils.java

/**
 * Invoke Business Rules with JSON content
 *
 * @param content The payload of itineraries to send to Business Rules.
 * @return A JSON string representing the output of Business Rules.
 *//*  w  ww .  j  a v a 2  s  .c  o m*/
public static String invokeRulesService(String json) throws Exception {
    PropertiesReader constants = PropertiesReader.getInstance();
    String username = constants.getStringProperty(USERNAME_KEY);
    String password = constants.getStringProperty(PASSWORD_KEY);
    String endpoint = constants.getStringProperty(EXECUTION_REST_URL_KEY)
            + constants.getStringProperty(RULE_APP_PATH_KEY);

    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
    CloseableHttpClient httpClient = HttpClientBuilder.create()
            .setDefaultCredentialsProvider(credentialsProvider).build();

    String responseString = "";

    try {
        HttpPost httpPost = new HttpPost(endpoint);
        httpPost.setHeader(HTTP.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString());
        StringEntity jsonEntity = new StringEntity(json, MessageUtils.ENCODING);
        httpPost.setEntity(jsonEntity);
        CloseableHttpResponse response = httpClient.execute(httpPost);

        try {
            HttpEntity entity = response.getEntity();
            responseString = EntityUtils.toString(entity, MessageUtils.ENCODING);
            EntityUtils.consume(entity);
        } finally {
            response.close();
        }
    } finally {
        httpClient.close();
    }
    return responseString;
}

From source file:com.nextdoor.bender.ipc.es.ElasticSearchTransport.java

@Override
protected ContentType getUncompressedContentType() {
    return ContentType.APPLICATION_JSON;
}

From source file:org.kitodo.data.elasticsearch.index.type.PropertyType.java

@SuppressWarnings("unchecked")
@Override//from  w  w  w.j  a  v  a  2s. c  o m
public HttpEntity createDocument(Property property) {

    JSONObject propertyObject = new JSONObject();
    propertyObject.put("title", property.getTitle());
    propertyObject.put("value", property.getValue());
    String creationDate = property.getCreationDate() != null ? formatDate(property.getCreationDate()) : null;
    propertyObject.put("creationDate", creationDate);
    propertyObject.put("processes", addObjectRelation(property.getProcesses()));
    propertyObject.put("templates", addObjectRelation(property.getTemplates()));
    propertyObject.put("workpieces", addObjectRelation(property.getWorkpieces()));

    String type = null;
    if (!property.getProcesses().isEmpty()) {
        type = "process";
    } else if (!property.getTemplates().isEmpty()) {
        type = "template";
    } else if (!property.getWorkpieces().isEmpty()) {
        type = "workpiece";
    }

    propertyObject.put("type", type);

    return new NStringEntity(propertyObject.toJSONString(), ContentType.APPLICATION_JSON);
}

From source file:org.kitodo.data.index.elasticsearch.type.TemplateType.java

@SuppressWarnings("unchecked")
@Override/*w  w w  .j  a  v a 2  s  .c o m*/
public HttpEntity createDocument(Template template) {

    LinkedHashMap<String, String> orderedTemplateMap = new LinkedHashMap<>();
    String process = template.getProcess() != null ? template.getProcess().getId().toString() : "null";
    orderedTemplateMap.put("process", process);

    JSONObject processObject = new JSONObject(orderedTemplateMap);

    JSONArray properties = new JSONArray();
    List<TemplateProperty> templateProperties = template.getProperties();
    for (TemplateProperty property : templateProperties) {
        JSONObject propertyObject = new JSONObject();
        propertyObject.put("title", property.getTitle());
        propertyObject.put("value", property.getValue());
        properties.add(propertyObject);
    }
    processObject.put("properties", properties);

    return new NStringEntity(processObject.toJSONString(), ContentType.APPLICATION_JSON);
}

From source file:com.srotya.tau.alerts.media.HttpService.java

/**
 * @param alert/*from  w w  w.  j ava2 s .  com*/
 * @throws AlertDeliveryException
 */
public void sendHttpCallback(Alert alert) throws AlertDeliveryException {
    try {
        CloseableHttpClient client = Utils.buildClient(alert.getTarget(), 3000, 3000);
        HttpPost request = new HttpPost(alert.getTarget());
        StringEntity body = new StringEntity(alert.getBody(), ContentType.APPLICATION_JSON);
        request.addHeader("content-type", "application/json");
        request.setEntity(body);
        HttpResponse response = client.execute(request);
        EntityUtils.consume(response.getEntity());
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode < 200 && statusCode >= 300) {
            throw exception;
        }
        client.close();
    } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException | IOException
            | AlertDeliveryException e) {
        throw exception;
    }
}