public Attachment addAttachment(final String assetId, final AttachmentSummary attSummary) throws IOException, BadVersionException, RequestFailureException { final Attachment attach = attSummary.getAttachment(); final String name = attSummary.getName(); // Info about the attachment goes into the URL String urlString = "/assets/" + assetId + "/attachments?name=" + name; if (attach.getType() != null) { urlString = urlString + "&type=" + attach.getType().toString(); } HttpURLConnection connection = createHttpURLConnectionToMassive(urlString); if (attSummary.getURL() == null) { writeMultiPart(assetId, attSummary, connection); } else { writeSinglePart(assetId, attSummary, connection); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); testResponseCode(connection); InputStream is = connection.getInputStream(); int len = 0; while ((len = is.read()) != -1) { baos.write((byte) len); } is.close(); baos.close(); Attachment attachment = JSONAssetConverter.readValue( new ByteArrayInputStream(baos.toByteArray()), Attachment.class); return attachment; }
private byte[] getStartBytes(final AttachmentSummary attSummary, String boundary) throws IOException { final String name = attSummary.getName(); final File fileToWrite = attSummary.getFile(); final Attachment attach = attSummary.getAttachment(); ByteArrayOutputStream startOutputStream = new ByteArrayOutputStream(); try { OutputStreamWriter writer = new OutputStreamWriter(startOutputStream, Charset.forName("UTF-8")); writer.write("--" + boundary + NEWLINE); writer.write("Content-Disposition: form-data; name=\"attachmentInfo\"" + NEWLINE); writer.write("Content-Type: application/json" + NEWLINE); writer.write(NEWLINE); writer.flush(); JSONAssetConverter.writeValue(startOutputStream, attach); writer.write(NEWLINE); writer.write("--" + boundary + NEWLINE); writer.write( "Content-Disposition: form-data; name=\"" + name + "\"; filename=\"" + fileToWrite.getName() + "\"" + NEWLINE); String uploadType = "application/binary"; // default value if (attach.getType() == null) { // Attachments should have a Type specified throw new IllegalArgumentException("Attachments must have a Type specified"); } else { switch (attach.getType()) { case LICENSE: uploadType = "text/html"; break; case LICENSE_AGREEMENT: case LICENSE_INFORMATION: uploadType = "text/plain"; break; default: break; } } writer.write("Content-Type: " + uploadType + NEWLINE); writer.write(NEWLINE); writer.close(); } finally { if (startOutputStream != null) { startOutputStream.close(); } } return startOutputStream.toByteArray(); }
/** * Returns the contents of an attachment * * @param assetId The ID of the asset owning the attachment * @param attachmentId The ID of the attachment * @return The input stream for the attachment * @throws IOException * @throws RequestFailureException */ public InputStream getAttachment(String assetId, String attachmentId) throws IOException, BadVersionException, RequestFailureException { // Get the URL for the attachment Attachment attachment = getAttachmentMetaData(assetId, attachmentId); // accept license for type CONTENT HttpURLConnection connection; if (attachment.getType() == Attachment.Type.CONTENT) { connection = createHttpURLConnection(attachment.getUrl() + "?license=agree"); } else { connection = createHttpURLConnection(attachment.getUrl()); } // If the attachment was a link and we have a basic auth userid + password specified // we are attempting to access the files staged from a protected site so authorise for it if (attachment.getLinkType() == LinkType.DIRECT) { if ((loginInfo.getAttachmentBasicAuthUserId() != null) && (loginInfo.getAttachmentBasicAuthPassword() != null)) { String userpass = loginInfo.getAttachmentBasicAuthUserId() + ":" + loginInfo.getAttachmentBasicAuthPassword(); String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary( userpass.getBytes(Charset.forName("UTF-8"))); connection.setRequestProperty("Authorization", basicAuth); } } connection.setRequestMethod("GET"); testResponseCode(connection); return connection.getInputStream(); }