public String startOnline() throws IOException { String uri = address + "api/active/receiver/start"; LinkedList<FormBodyPart> partsList = new LinkedList<>(); partsList.add(new FormBodyPart("token", new StringBody(token))); partsList.add(new FormBodyPart("projectKey", new StringBody(project))); partsList.add(new FormBodyPart("title", new StringBody(title))); JSONObject obj = queryObject(createPost(uri, partsList), 201); return address + "gui/active/" + obj.optString("OnlineID", "N/A") + "/"; }
public LoadosophiaUploadResults sendFiles(File targetFile, LinkedList<String> perfMonFiles) throws IOException { notifier.notifyAbout("Starting upload to BM.Sense"); LoadosophiaUploadResults results = new LoadosophiaUploadResults(); LinkedList<FormBodyPart> partsList = getUploadParts(targetFile, perfMonFiles); JSONObject res = queryObject(createPost(address + "api/files", partsList), 201); int queueID = Integer.parseInt(res.getString("QueueID")); results.setQueueID(queueID); int testID = getTestByUpload(queueID); results.setTestID(testID); setTestTitleAndColor(testID, title.trim(), colorFlag); results.setRedirectLink(address + "gui/" + testID + "/"); return results; }
/** * Classify the supplied title and body as if they were a document * * @param fileName The file name of the document to classify * @param body The document body * @param title The document title * @param metadata Map containing metadata * @return the classifications as returned by classification server. * @throws ClassificationException Classification exception */ @Deprecated public Map<String, Collection<ClassificationScore>> classifyDocument( FileName fileName, Body body, Title title, Map<String, Collection<String>> metadata) throws ClassificationException { logger.debug("Treating document: '" + title.getValue() + "'"); // If there is no body, then don't bother attempting to classify the // document if ((body == null) || (body.getValue() == null) || (body.getValue().trim().length() == 0)) { Result result = new Result(null); return result.getAllClassifications(); } Collection<FormBodyPart> parts = new ArrayList<FormBodyPart>(); addTitle(parts, title); addByteArray(parts, body); addMetadata(parts, metadata); return getClassifications(parts).getAllClassifications(); }
/** * Classify the supplied title and body as if they were a document * @param fileName The file name of the document to classify * @param body The document body * @param title The document title * @param metadata Map containing metadata * @return the classifications as returned by classification server. * @throws ClassificationException Classification exception */ public Result getClassifiedDocument(FileName fileName, Body body, Title title, Map<String, Collection<String>> metadata) throws ClassificationException { logger.debug("Treating document: '" + title.getValue() + "'"); // If there is no body, then don't bother attempting to classify the // document if ((body == null) || (body.getValue() == null) || (body.getValue().trim().length() == 0)) { Result result = new Result(null); return result; } Collection<FormBodyPart> parts = new ArrayList<FormBodyPart>(); addTitle(parts, title); addMetadata(parts, metadata); addByteArray(parts, body, fileName); return getClassifications(parts); }
public byte[] getClassificationServerResponse(FileName filename, Body body, Title title, Map<String, Collection<String>> metadata) throws ClassificationException { logger.debug("Treating document: '" + title.getValue() + "'"); // If there is no body, then don't bother attempting to classify the // document if ((body == null) || (body.getValue() == null) || (body.getValue().trim().length() == 0)) { return new byte[0]; } Collection<FormBodyPart> parts = new ArrayList<FormBodyPart>(); addTitle(parts, title); addMetadata(parts, metadata); addByteArray(parts, body, filename); return getClassificationServerResponse(parts); }
private void addMetadata(Collection<FormBodyPart> parts, Map<String, Collection<String>> metadata) { if (metadata != null) { for (String name : metadata.keySet()) { Collection<String> values = metadata.get(name); if (values != null) { int m = 0; for (String value : values) { if (m == 0) parts.add(new FormBodyPart("meta_" + name, getStringBody(value))); else parts.add(new FormBodyPart("meta_" + name + "__" + m, getStringBody(value))); m++; } } } } }
private Collection<FormBodyPart> getParts( ClassificationConfiguration classificationConfiguration) { ArrayList<FormBodyPart> partsList = new ArrayList<FormBodyPart>(); for (String parameterName : classificationConfiguration.getAdditionalParameters().keySet()) { String value = classificationConfiguration.getAdditionalParameters().get(parameterName); if ((value != null) && (value.length() > 0)) { partsList.add(new FormBodyPart(parameterName, getStringBody(value))); } } if (classificationConfiguration.isSingleArticle()) partsList.add(new FormBodyPart("singlearticle", getStringBody("on"))); if (classificationConfiguration.isMultiArticle()) partsList.add(new FormBodyPart("multiarticle", getStringBody("on"))); if (classificationConfiguration.isFeedback()) partsList.add(new FormBodyPart("feedback", getStringBody("on"))); if (classificationConfiguration.isStylesheet()) partsList.add(new FormBodyPart("stylesheet", getStringBody("on"))); if (classificationConfiguration.isUseGeneratedKeys()) partsList.add(new FormBodyPart("use_generated_keys", getStringBody("on"))); if (classificationConfiguration.isReturnHashCode()) partsList.add(new FormBodyPart("return_hash", getStringBody("on"))); return partsList; }
private static FormBodyPart newFileBody(final String parameterName, final String fileName, final String fileContent) throws Exception { return new FormBodyPart( parameterName, new StringBody(fileContent, ContentType.TEXT_PLAIN) { @Override public String getFilename() { return fileName; } @Override public String getTransferEncoding() { return "binary"; } @Override public String getMimeType() { return ""; } @Override public String getCharset() { return null; } }); }
/** * Encodes the request as a multipart message for sending to the server. * * @return MultipartEntity suitable for transmission in an HTTP request. */ @Override public HttpEntity getContent() { // Add a part with as a form List<NameValuePair> parms = new ArrayList<NameValuePair>(); getHttpQueryParms(parms); String parmsText = URLEncodedUtils.format(parms, Consts.UTF_8); MultipartEntityBuilder builder = MultipartEntityBuilder.create() .addTextBody("parms", parmsText, ContentType.create("application/x-www-form-urlencoded", Consts.UTF_8)); for (FormBodyPart part: attachments) builder.addPart(part.getName(), part.getBody()); return builder.build(); }
void doWriteTo( final OutputStream out, final boolean writeContent) throws IOException { final ByteArrayBuffer boundaryEncoded = encode(this.charset, this.boundary); for (final FormBodyPart part: getBodyParts()) { writeBytes(TWO_DASHES, out); writeBytes(boundaryEncoded, out); writeBytes(CR_LF, out); formatMultipartHeader(part, out); writeBytes(CR_LF, out); if (writeContent) { part.getBody().writeTo(out); } writeBytes(CR_LF, out); } writeBytes(TWO_DASHES, out); writeBytes(boundaryEncoded, out); writeBytes(TWO_DASHES, out); writeBytes(CR_LF, out); }
/** * Determines the total length of the multipart content (content length of * individual parts plus that of extra elements required to delimit the parts * from one another). If any of the @{link BodyPart}s contained in this object * is of a streaming entity of unknown length the total length is also unknown. * <p> * This method buffers only a small amount of data in order to determine the * total length of the entire entity. The content of individual parts is not * buffered. * </p> * * @return total length of the multipart entity if known, {@code -1} otherwise. */ public long getTotalLength() { long contentLen = 0; for (final FormBodyPart part: getBodyParts()) { final ContentBody body = part.getBody(); final long len = body.getContentLength(); if (len >= 0) { contentLen += len; } else { return -1; } } final ByteArrayOutputStream out = new ByteArrayOutputStream(); try { doWriteTo(out, false); final byte[] extra = out.toByteArray(); return contentLen + extra.length; } catch (final IOException ex) { // Should never happen return -1; } }
@Override protected HttpUriRequest createRequest() throws IOException { MultipartEntity entity = new MultipartEntity(); List<NameValuePair> dataList = getData(); for (NameValuePair d : dataList) { entity.addPart(new FormBodyPart(d.getName(), new StringBody(d.getValue(), Charset.forName("utf-8")))); } for (Map.Entry<String, InputStreamBody> entry : files.entrySet()) { entity.addPart(new FormBodyPart(entry.getKey(), entry.getValue())); } final HttpPost request = new HttpPost(url); if(this.sendProgressListener != null) { CountingRequestEntity countingEntity = new CountingRequestEntity(entity, this.sendProgressListener); request.setEntity(countingEntity); } else { request.setEntity(entity); } return request; }
/** * Create Post Request with FormBodyPart body */ public HttpPost createPost(String uri, LinkedList<FormBodyPart> partsList) { HttpPost postRequest = new HttpPost(uri); MultipartEntity multipartRequest = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); for (FormBodyPart part : partsList) { multipartRequest.addPart(part); } postRequest.setEntity(multipartRequest); return postRequest; }
public void sendOnlineData(JSONArray data) throws IOException { String uri = address + "api/active/receiver/data"; LinkedList<FormBodyPart> partsList = new LinkedList<>(); String dataStr = data.toString(); log.debug("Sending active test data: " + dataStr); partsList.add(new FormBodyPart("data", new StringBody(dataStr))); query(createPost(uri, partsList), 202); }
private LinkedList<FormBodyPart> getUploadParts(File targetFile, LinkedList<String> perfMonFiles) throws IOException { if (targetFile.length() == 0) { throw new IOException("Cannot send empty file to BM.Sense"); } log.info("Preparing files to send"); LinkedList<FormBodyPart> partsList = new LinkedList<>(); partsList.add(new FormBodyPart("projectKey", new StringBody(project))); partsList.add(new FormBodyPart("jtl_file", new FileBody(gzipFile(targetFile)))); Iterator<String> it = perfMonFiles.iterator(); int index = 0; while (it.hasNext()) { File perfmonFile = new File(it.next()); if (!perfmonFile.exists()) { log.warn("File not exists, skipped: " + perfmonFile.getAbsolutePath()); continue; } if (perfmonFile.length() == 0) { log.warn("Empty file skipped: " + perfmonFile.getAbsolutePath()); continue; } partsList.add(new FormBodyPart("perfmon_" + index, new FileBody(gzipFile(perfmonFile)))); index++; } return partsList; }
/** * Classify the supply binary array as if it were the contents of a file * * @param data The data to classify * @param fileName String containing the name of the file * @param title The document title * @param metadata Map containing metadata * @return the classifications as returned by classification server. * @throws ClassificationException Classification exception */ @Deprecated public Map<String, Collection<ClassificationScore>> classifyBinary( byte[] data, String fileName, Title title, Map<String, Collection<String>> metadata) throws ClassificationException { logger.debug("Treating file: '" + fileName + "'"); Collection<FormBodyPart> parts = new ArrayList<FormBodyPart>(); addTitle(parts, title); addMetadata(parts, metadata); addByteArray(parts, data, fileName); return getClassifications(parts).getAllClassifications(); }
/** * Return the version of Classification Server (as displayed in the Test * Interface) * * @return The version * @throws ClassificationException Classification exception */ public String getVersion() throws ClassificationException { logger.debug("getVersion"); if (version == null) { Collection<FormBodyPart> parts = new ArrayList<FormBodyPart>(); parts.add(new FormBodyPart("body", getStringBody("squirmish"))); getClassificationServerResponse(parts); } return version; }
/** * Return the information that CS makes available. * @return Classification Server information * @throws ClassificationException Classification exception */ public CSInfo getInfo() throws ClassificationException { logger.debug("getInfo"); Collection<FormBodyPart> parts = new ArrayList<FormBodyPart>(); parts.add(new FormBodyPart("operation", getStringBody("info"))); CSInfo csInfo = new CSInfo(getClassificationServerResponse(parts)); return csInfo; }
/** * Classify the supplied file * * @param inputFile The input file to classify * @param fileType File type of "inputFile". If the file type is not supplied (i.e. is null) then it will be guessed by classification server. * @param title The document title * @param metadata Map containing metadata * @param outMetadata Container populated with the extracted metadata * @return the classifications as returned by classification server. * @throws ClassificationException Classification exception */ @Deprecated public Map<String, Collection<ClassificationScore>> classifyFile( File inputFile, String fileType, Title title, Map<String, Collection<String>> metadata, Map<String, String> outMetadata) throws ClassificationException { logger.debug("Treating file: '" + inputFile + "'"); Collection<FormBodyPart> parts = new ArrayList<FormBodyPart>(); addFile(parts, inputFile, fileType); addTitle(parts, title); addMetadata(parts, metadata); return getClassifications(parts, outMetadata).getAllClassifications(); }
/** * Classify the supplied url with the extra metadata * * @param url The URL to classify * @param title The document title * @param metadata Map containing metadata * @return the classifications as returned by classification server. * @throws ClassificationException Classification exception */ public Result getClassifiedDocument(URL url, Title title, Map<String, Collection<String>> metadata) throws ClassificationException { if (logger.isDebugEnabled()) logger.debug("Treating URL: '" + url.toExternalForm() + "'"); Collection<FormBodyPart> parts = new ArrayList<FormBodyPart>(); addTitle(parts, title); addMetadata(parts, metadata); parts.add(new FormBodyPart("path", getStringBody(url.toExternalForm()))); return getClassifications(parts); }
/** * Return the classification records for all requests between the two * supplied dates * * @param startTime The earliest possible date for returned results * @param endTime The latest possible date for returned results * @return One record for each document classified in that date range * @throws ClassificationException Classification exception */ public Collection<ClassificationRecord> getClassificationHistory( Date startTime, Date endTime) throws ClassificationException { logger.info("getClassificationHistory - entry"); SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ssZ"); ArrayList<FormBodyPart> partsList = new ArrayList<FormBodyPart>(); partsList.add(new FormBodyPart("start_time", getStringBody(simpleDateFormat.format(startTime)))); partsList.add(new FormBodyPart("finish_time", getStringBody(simpleDateFormat.format(endTime)))); partsList.add(new FormBodyPart("operation", getStringBody("getclassificationhistory"))); ClassificationHistory classificationHistory = new ClassificationHistory(getClassificationServerResponse(partsList)); return classificationHistory.getClassificationRecords(); }
/** * Return the rulebase classes that are currently configured on the * classification server instance * * @return List of rulebases classes * @throws ClassificationException Classification exception */ public Collection<RulebaseClass> getRulebaseClasses() throws ClassificationException { logger.debug("getRulebaseClasses - entry"); ArrayList<FormBodyPart> partsList = new ArrayList<FormBodyPart>(); partsList.add(new FormBodyPart("operation", getStringBody("listrulenetclasses"))); RulebaseClassSet rulebaseClassSet = new RulebaseClassSet( getClassificationServerResponse(partsList)); return rulebaseClassSet.getRulebaseClasses(); }
/** * Return the list of languages available on the cs instance * * @return List of languages * @throws ClassificationException Classification exception */ public Collection<Language> getLanguages() throws ClassificationException { logger.debug("getLanguages - entry"); ArrayList<FormBodyPart> partsList = new ArrayList<FormBodyPart>(); partsList.add(new FormBodyPart("operation", getStringBody( "listlanguages"))); LanguageSet langSet = new LanguageSet(getClassificationServerResponse(partsList)); return langSet.getLanguages(); }
/** * Return the map of default parameter values * * @return Map of default parameter values * @throws ClassificationException Classification exception */ public Map<String, Parameter> getDefaults() throws ClassificationException { logger.debug("getDefaults - entry"); ArrayList<FormBodyPart> partsList = new ArrayList<FormBodyPart>(); partsList.add(new FormBodyPart("operation", getStringBody("getparameterdefaults"))); Defaults defaults = new Defaults( getClassificationServerResponse(partsList)); return defaults.getDefaults(); }
/** * Return in a structured form the output of the classification process * * @param data Data to classify * @param fileName A string containing the name of the file to classify * @return The structured result of the classification * @throws ClassificationException Classification exception */ public Result getClassifiedDocument(byte[] data, String fileName) throws ClassificationException { logger.debug("Treating byte array: '" + fileName + "'"); Collection<FormBodyPart> parts = new ArrayList<FormBodyPart>(); if ((data == null) || (data.length == 0)) return new Result(null); addByteArray(parts, data, fileName); return new Result(getClassificationServerResponse(parts)); }
/** * Return in a structured form the output of the classification process * * @param data Data to classify * @param fileName A string containing the name of the file to classify * @param title The document title * @param metadata Map containing metadata * @return The structured result of the classification * @throws ClassificationException Classification exception */ public Result getClassifiedDocument(byte[] data, String fileName, Title title, Map<String, Collection<String>> metadata) throws ClassificationException { logger.debug("Treating file: '" + fileName + "'"); Collection<FormBodyPart> parts = new ArrayList<FormBodyPart>(); addTitle(parts, title); addMetadata(parts, metadata); addByteArray(parts, data, fileName); return new Result(getClassificationServerResponse(parts)); }
/** * Return in a structured form the output of the classification process * * @param inputFile The input file to classify * @param fileType File type of "inputFile". If the file type is not supplied (i.e. is null) then it will be guessed by classification server. * @param title The document title * @param metadata Map containing metadata * @return The structured result of the classification * @throws ClassificationException Classification exception */ public Result getClassifiedDocument(File inputFile, String fileType, Title title, Map<String, Collection<String>> metadata) throws ClassificationException { logger.debug("Treating file: '" + inputFile.getName() + "'"); Collection<FormBodyPart> parts = new ArrayList<FormBodyPart>(); addTitle(parts, title); addMetadata(parts, metadata); addFile(parts, inputFile, fileType); return new Result(getClassificationServerResponse(parts)); }
private void addByteArray(Collection<FormBodyPart> parts, Body body, FileName filename) { if (filename == null) { parts.add(new FormBodyPart("body", getStringBody(body.getValue()))); } else { addByteArray(parts, body.getValue().getBytes(Charset.forName("UTF-8")), filename.getValue()); } }
private void addFile(Collection<FormBodyPart> parts, File inputFile, String fileType) throws ClassificationException { if (inputFile == null) { throw new ClassificationException("Null input file provided"); } if (!inputFile.exists()) { throw new ClassificationException("Input file not found: " + inputFile.getAbsolutePath()); } if (fileType != null) { parts.add(new FormBodyPart("UploadFile", new FileBody(inputFile))); // TODO Can we access the content type? } else { parts.add(new FormBodyPart("UploadFile", new FileBody(inputFile))); } }
private Result getClassifications(Collection<FormBodyPart> partsList, Map<String, String> outMeta) throws ClassificationException { Result result = new Result(getClassificationServerResponse(partsList)); if ((result != null) && (result.getMetadata() != null) && (outMeta != null)) { for (String meta : result.getMetadata().keySet()){ outMeta.put(meta, result.getMetadata().get(meta)); } } return result; }
public byte[] getClassificationServerResponse(File inputFile, String fileType, Title title, Map<String, Collection<String>> metadata) throws ClassificationException { logger.debug("Treating file: '" + inputFile + "'"); Collection<FormBodyPart> parts = new ArrayList<FormBodyPart>(); addFile(parts, inputFile, fileType); addTitle(parts, title); addMetadata(parts, metadata); return getClassificationServerResponse(parts); }
public static MultipartEntity getMultipartEntity(List<FormBodyPart> formBodyParts) { MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, Charset.forName(HTTP.UTF_8)); for (FormBodyPart formBodyPart : formBodyParts) { DebugLog.d(TAG, formBodyPart.getHeader().toString()); multipartEntity.addPart(formBodyPart); } return multipartEntity; }
private Map<String, ContentBody> getBodyMap(HttpUriRequest request) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { HttpPost httpPost = (HttpPost) request; MultipartEntity entity = (MultipartEntity) httpPost.getEntity(); Field multipartField = MultipartEntity.class.getDeclaredField("multipart"); multipartField.setAccessible(true); HttpMultipart httpMultipart = (HttpMultipart) multipartField.get(entity); List<FormBodyPart> bodyParts = httpMultipart.getBodyParts(); Map<String, ContentBody> parts = new HashMap<String, ContentBody>(); for (FormBodyPart part : bodyParts) { parts.put(part.getName(), part.getBody()); } return parts; }
/** * Write the multipart header fields; depends on the style. */ protected void formatMultipartHeader( final FormBodyPart part, final OutputStream out) throws IOException { // For strict, we output all fields with MIME-standard encoding. final Header header = part.getHeader(); for (final MinimalField field: header) { writeField(field, out); } }
public MultipartEntityBuilder addPart(FormBodyPart bodyPart) { if (bodyPart == null) { return this; } if (this.bodyParts == null) { this.bodyParts = new ArrayList<FormBodyPart>(); } if(mtom) { Header header = bodyPart.getHeader(); String contentID; String fileName = bodyPart.getBody().getFilename(); header.removeFields("Content-Disposition"); if(fileName == null) { contentID = "<"+bodyPart.getName()+">"; } else { bodyPart.addField("Content-Disposition", "attachment; name=\""+fileName+"\"; filename=\""+fileName+"\""); contentID = "<"+fileName+">"; } bodyPart.addField("Content-ID", contentID); if(firstPart == null) firstPart = contentID; } this.bodyParts.add(bodyPart); return this; }
private MultipartEntity buildEntity() { String boundaryCopy = boundary; if (boundaryCopy == null && contentType != null) { boundaryCopy = contentType.getParameter("boundary"); } if (boundaryCopy == null) { boundaryCopy = generateBoundary(); } Charset charsetCopy = charset; if (charsetCopy == null && contentType != null) { charsetCopy = contentType.getCharset(); } List<NameValuePair> paramsList = new ArrayList<NameValuePair>(5); paramsList.add(new BasicNameValuePair("boundary", boundaryCopy)); if (charsetCopy != null) { paramsList.add(new BasicNameValuePair("charset", charsetCopy.name())); } String subtypeCopy = DEFAULT_SUBTYPE; if(mtom) { paramsList.add(new BasicNameValuePair("type", "application/xop+xml")); paramsList.add(new BasicNameValuePair("start", firstPart)); paramsList.add(new BasicNameValuePair("start-info", firstPart)); subtypeCopy = MTOM_SUBTYPE; } NameValuePair[] params = paramsList.toArray(new NameValuePair[paramsList.size()]); ContentType contentTypeCopy = contentType != null ? contentType.withParameters(params) : ContentType.create("multipart/" + subtypeCopy, params); List<FormBodyPart> bodyPartsCopy = bodyParts != null ? new ArrayList<FormBodyPart>(bodyParts) : Collections.<FormBodyPart>emptyList(); MultipartForm form = new MultipartForm(charsetCopy, boundaryCopy, bodyPartsCopy); return new MultipartEntity(form, contentTypeCopy, form.getTotalLength()); }
protected FormBodyPart createMultipartBodypart(String name, String message, String contentType) { FormBodyPartBuilder bodyPart = FormBodyPartBuilder.create() .setName(name) .setBody(new StringBody(message, ContentType.create(contentType, getCharSet()))); if (StringUtils.isNotEmpty(getMtomContentTransferEncoding())) bodyPart.setField(MIME.CONTENT_TRANSFER_ENC, getMtomContentTransferEncoding()); return bodyPart.build(); }
public void endOnline(String redirectLink) throws IOException { String uri = address + "api/active/receiver/stop"; LinkedList<FormBodyPart> partsList = new LinkedList<>(); partsList.add(new FormBodyPart("redirect", new StringBody(redirectLink))); query(createPost(uri, partsList), 205); }
private void addTitle(Collection<FormBodyPart> parts, Title title) { if ((title != null) && (title.getValue() != null) && (title.getValue().length() > 0)) { parts.add(new FormBodyPart("title", getStringBody(title.getValue()))); } }
private void addByteArray(Collection<FormBodyPart> parts, Body body) { parts.add(new FormBodyPart("body", getStringBody(body.getValue()))); }