/** * 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(); }
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; }
/** * This will delete an asset and all its attachments * * @param assetId The id of the asset * @return <code>true</code> if the asset and all its attachments are deleted. Note this is not * atomic so some attachments may be deleted and <code>false</code> returned. * @throws IOException * @throws RequestFailureException */ public void deleteAssetAndAttachments(String assetId) throws IOException, RequestFailureException { Asset ass = getUnverifiedAsset(assetId); List<Attachment> attachments = ass.getAttachments(); if (attachments != null) { for (Attachment attachment : attachments) { deleteAttachment(assetId, attachment.get_id()); } } // Now delete the asset deleteAsset(assetId); }
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 meta data about an attachment * * @param assetId The ID of the asset owning the attachment * @param attachmentId The ID of the attachment * @return The attachment meta data * @throws IOException * @throws RequestFailureException */ public Attachment getAttachmentMetaData(String assetId, String attachmentId) throws IOException, BadVersionException, RequestFailureException { // At the moment can only get all attachments Asset ass = getAsset(assetId); List<Attachment> allAttachments = ass.getAttachments(); for (Attachment attachment : allAttachments) { if (attachmentId.equals(attachment.get_id())) { return attachment; } } // Didn't find it so just return null return null; }
/** * This method will update an existing attachment on an asset. Note that Massive currently doesn't * support update attachment so this will do a delete and an add. * * @param assetId The ID of the asset that the attachment is attached to * @param name The name of the attachment to update * @param file The file to attach * @param attach Attachment metadata * @return * @throws IOException * @throws RequestFailureException */ public Attachment updateAttachment(String assetId, AttachmentSummary summary) throws IOException, BadVersionException, RequestFailureException { // First find the attachment to update // TODO update when massive supports proper updates to attachments Asset ass = getAsset(assetId); List<Attachment> attachments = ass.getAttachments(); if (attachments != null) { for (Attachment attachment : attachments) { if (attachment.getName().equals(summary.getName())) { this.deleteAttachment(assetId, attachment.get_id()); break; } } } return this.addAttachment(assetId, summary); }