List of usage examples for org.apache.http.nio.entity NStringEntity NStringEntity
public NStringEntity(final String s, String charset) throws UnsupportedEncodingException
From source file:org.kitodo.data.index.elasticsearch.type.TemplateType.java
@SuppressWarnings("unchecked") @Override//from w ww. ja va 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:org.kitodo.data.index.elasticsearch.type.WorkpieceType.java
@SuppressWarnings("unchecked") @Override/*from ww w .ja va 2 s . c o m*/ public HttpEntity createDocument(Workpiece workpiece) { LinkedHashMap<String, String> orderedWorkpieceMap = new LinkedHashMap<>(); String process = workpiece.getProcess() != null ? workpiece.getProcess().getId().toString() : "null"; orderedWorkpieceMap.put("process", process); JSONObject processObject = new JSONObject(orderedWorkpieceMap); JSONArray properties = new JSONArray(); List<WorkpieceProperty> workpieceProperties = workpiece.getProperties(); for (WorkpieceProperty property : workpieceProperties) { 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:marytts.server.http.InfoRequestHandler.java
@Override protected void handleClientRequest(String absPath, Map<String, String> queryItems, HttpResponse response, Address serverAddressAtClient) throws IOException { // Individual info request String infoResponse = handleInfoRequest(absPath, queryItems, response); if (infoResponse == null) { // error condition, handleInfoRequest has set an error message return;//from ww w .ja v a2 s .c o m } response.setStatusCode(HttpStatus.SC_OK); try { NStringEntity entity = new NStringEntity(infoResponse, "UTF-8"); entity.setContentType("text/plain; charset=UTF-8"); response.setEntity(entity); } catch (UnsupportedEncodingException e) { } }
From source file:org.kitodo.data.elasticsearch.index.type.ProcessType.java
@SuppressWarnings("unchecked") @Override/*from w ww . j a va 2 s.c o m*/ public HttpEntity createDocument(Process process) { JSONObject processObject = new JSONObject(); processObject.put("title", process.getTitle()); processObject.put("outputName", process.getOutputName()); String creationDate = process.getCreationDate() != null ? formatDate(process.getCreationDate()) : null; processObject.put("creationDate", creationDate); processObject.put("wikiField", process.getWikiField()); processObject.put("sortHelperStatus", process.getSortHelperStatus()); processObject.put("sortHelperImages", process.getSortHelperImages()); processObject.put("processBaseUri", process.getProcessBaseUri()); processObject.put("template", process.isTemplate()); Integer project = process.getProject() != null ? process.getProject().getId() : null; processObject.put("project", project); Integer ruleset = process.getRuleset() != null ? process.getRuleset().getId() : null; processObject.put("ruleset", ruleset); Integer docket = process.getDocket() != null ? process.getDocket().getId() : null; processObject.put("docket", docket); processObject.put("batches", addObjectRelation(process.getBatches())); processObject.put("workpieces", addObjectRelation(process.getWorkpieces())); processObject.put("tasks", addObjectRelation(process.getTasks())); processObject.put("properties", addObjectRelation(process.getProperties())); return new NStringEntity(processObject.toJSONString(), ContentType.APPLICATION_JSON); }
From source file:org.apache.beam.sdk.io.elasticsearch.ElasticSearchIOTestUtils.java
/** * Synchronously deletes the target if it exists and then (re)creates it as a copy of source * synchronously./*from w w w . j a v a2 s . com*/ */ static void copyIndex(RestClient restClient, String source, String target) throws IOException { deleteIndex(restClient, target); HttpEntity entity = new NStringEntity( String.format("{\"source\" : { \"index\" : \"%s\" }, \"dest\" : { \"index\" : \"%s\" } }", source, target), ContentType.APPLICATION_JSON); restClient.performRequest("POST", "/_reindex", Collections.EMPTY_MAP, entity); }
From source file:org.kitodo.data.index.elasticsearch.type.UserType.java
@SuppressWarnings("unchecked") @Override/*from w w w . j a v a 2 s . c o m*/ public HttpEntity createDocument(User user) { LinkedHashMap<String, String> orderedUserMap = new LinkedHashMap<>(); orderedUserMap.put("name", user.getName()); orderedUserMap.put("surname", user.getSurname()); orderedUserMap.put("login", user.getLogin()); orderedUserMap.put("ldapLogin", user.getLdapLogin()); orderedUserMap.put("active", String.valueOf(user.isActive())); orderedUserMap.put("location", user.getLocation()); orderedUserMap.put("metadataLanguage", user.getMetadataLanguage()); String ldapGroup = user.getLdapGroup() != null ? user.getLdapGroup().getId().toString() : "null"; orderedUserMap.put("ldapGroup", ldapGroup); JSONObject userObject = new JSONObject(orderedUserMap); JSONArray userGroups = new JSONArray(); List<UserGroup> userUserGroups = user.getUserGroups(); for (UserGroup userGroup : userUserGroups) { JSONObject userGroupObject = new JSONObject(); userGroupObject.put("id", userGroup.getId().toString()); userGroups.add(userGroupObject); } userObject.put("userGroups", userGroups); JSONArray properties = new JSONArray(); List<UserProperty> userProperties = user.getProperties(); for (UserProperty property : userProperties) { JSONObject propertyObject = new JSONObject(); propertyObject.put("title", property.getTitle()); propertyObject.put("value", property.getValue()); properties.add(propertyObject); } userObject.put("properties", properties); return new NStringEntity(userObject.toJSONString(), ContentType.APPLICATION_JSON); }
From source file:org.kitodo.data.index.elasticsearch.type.ProcessType.java
@SuppressWarnings("unchecked") @Override//from www. j a va 2 s . c om public HttpEntity createDocument(Process process) { LinkedHashMap<String, String> orderedProcessMap = new LinkedHashMap<>(); orderedProcessMap.put("name", process.getTitle()); orderedProcessMap.put("outputName", process.getOutputName()); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); String creationDate = process.getCreationDate() != null ? dateFormat.format(process.getCreationDate()) : null; orderedProcessMap.put("creationDate", creationDate); orderedProcessMap.put("wikiField", process.getWikiField()); String project = process.getProject() != null ? process.getProject().getId().toString() : "null"; orderedProcessMap.put("project", project); String ruleset = process.getRuleset() != null ? process.getRuleset().getId().toString() : "null"; orderedProcessMap.put("ruleset", ruleset); String ldapGroup = process.getDocket() != null ? process.getDocket().getId().toString() : "null"; orderedProcessMap.put("ldapGroup", ldapGroup); JSONObject processObject = new JSONObject(orderedProcessMap); JSONArray properties = new JSONArray(); List<ProcessProperty> processProperties = process.getProperties(); for (ProcessProperty property : processProperties) { 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.mycompany.allasync.NHttpRequestHandler.java
private void handleInternal(final HttpRequest request, final HttpResponse response, final HttpContext context) throws HttpException, IOException { String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH); if (!method.equals("GET") && !method.equals("HEAD") && !method.equals("POST")) { throw new MethodNotSupportedException(method + " method not supported"); }/*w w w . jav a 2s .c o m*/ String target = request.getRequestLine().getUri(); final File file = new File(this.docRoot, URLDecoder.decode(target, "UTF-8")); if (!file.exists()) { response.setStatusCode(HttpStatus.SC_NOT_FOUND); NStringEntity entity = new NStringEntity( "<html><body><h1>File" + file.getPath() + " not found</h1></body></html>", ContentType.create("text/html", "UTF-8")); response.setEntity(entity); System.out.println("File " + file.getPath() + " not found"); } else if (!file.canRead() || file.isDirectory()) { response.setStatusCode(HttpStatus.SC_FORBIDDEN); NStringEntity entity = new NStringEntity("<html><body><h1>Access denied</h1></body></html>", ContentType.create("text/html", "UTF-8")); response.setEntity(entity); System.out.println("Cannot read file " + file.getPath()); } else { NHttpConnection conn = (NHttpConnection) context.getAttribute(ExecutionContext.HTTP_CONNECTION); response.setStatusCode(HttpStatus.SC_OK); NFileEntity body = new NFileEntity(file, ContentType.create("text/html")); response.setEntity(body); System.out.println(conn + ": serving file " + file.getPath()); } }
From source file:org.kitodo.data.elasticsearch.search.SearchRestClient.java
/** * Count amount of documents responding to given query. * * @param query//from www.ja v a 2 s . co m * to find a document * @return http entity as String */ String countDocuments(QueryBuilder query) throws CustomResponseException, DataException { String wrappedQuery = "{\n \"query\": " + query.toString() + "\n}"; HttpEntity entity = new NStringEntity(wrappedQuery, ContentType.APPLICATION_JSON); return performRequest(entity, HttpMethod.GET, "_count"); }
From source file:org.kitodo.data.elasticsearch.index.type.ProjectType.java
@SuppressWarnings("unchecked") @Override//from ww w . j a v a 2 s .c om public HttpEntity createDocument(Project project) { JSONObject projectObject = new JSONObject(); projectObject.put("title", project.getTitle()); String startDate = project.getStartDate() != null ? formatDate(project.getStartDate()) : null; projectObject.put("startDate", startDate); String endDate = project.getEndDate() != null ? formatDate(project.getEndDate()) : null; projectObject.put("endDate", endDate); projectObject.put("numberOfPages", project.getNumberOfPages()); projectObject.put("numberOfVolumes", project.getNumberOfVolumes()); projectObject.put("fileFormatDmsExport", project.getFileFormatDmsExport()); projectObject.put("fileFormatInternal", project.getFileFormatInternal()); String archived = project.getProjectIsArchived() != null ? project.getProjectIsArchived().toString() : null; projectObject.put("archived", archived); projectObject.put("processes", addObjectRelation(project.getProcesses())); projectObject.put("users", addObjectRelation(project.getUsers())); JSONArray projectFileGroups = new JSONArray(); List<ProjectFileGroup> projectProjectFileGroups = project.getProjectFileGroups(); for (ProjectFileGroup projectFileGroup : projectProjectFileGroups) { JSONObject projectFileGroupObject = new JSONObject(); projectFileGroupObject.put("name", projectFileGroup.getName()); projectFileGroupObject.put("path", projectFileGroup.getPath()); projectFileGroupObject.put("mimeType", projectFileGroup.getMimeType()); projectFileGroupObject.put("suffix", projectFileGroup.getSuffix()); projectFileGroupObject.put("folder", projectFileGroup.getFolder()); projectFileGroups.add(projectFileGroupObject); } projectObject.put("projectFileGroups", projectFileGroups); return new NStringEntity(projectObject.toJSONString(), ContentType.APPLICATION_JSON); }