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(); }
/** * This method will update the state of an object by taking the supplied action. * * @param assetId The ID of the asset to update * @param action The action to take to modify the state * @return <code>true</code> if the update was successful (currently always returns <code>true * </code> from Massive) * @throws IOException * @throws RequestFailureException */ public void updateState(String assetId, Action action) throws IOException, RequestFailureException { StateAction newState = new StateAction(action); HttpURLConnection connection = createHttpURLConnectionToMassive("/assets/" + assetId + "/state"); connection.setRequestMethod("PUT"); connection.setRequestProperty("Content-Type", "application/json"); connection.setDoOutput(true); JSONAssetConverter.writeValue(connection.getOutputStream(), newState); // Make sure it was ok testResponseCode(connection); }
/** * Adds an asset into Massive. Note that Massive will set some fields (such as ID) so it is * important to switch to the returned object after calling this method. * * @param asset The asset to add, it will not be modified by this method * @return The asset with information added by Massive * @throws IOException */ public Asset addAsset(Asset asset) throws IOException, BadVersionException, RequestFailureException { HttpURLConnection connection = createHttpURLConnectionToMassive("/assets"); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setDoOutput(true); JSONAssetConverter.writeValue(connection.getOutputStream(), asset); testResponseCode(connection); Asset returnedAsset = JSONAssetConverter.readValue(connection.getInputStream()); returnedAsset = getAsset(returnedAsset.get_id()); return returnedAsset; }
private void writeSinglePart( final String assetId, final AttachmentSummary attSummary, HttpURLConnection connection) throws IOException, BadVersionException, RequestFailureException { final Attachment attach = attSummary.getAttachment(); connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestMethod("POST"); connection.setDoOutput(true); OutputStream httpStream = connection.getOutputStream(); JSONAssetConverter.writeValue(httpStream, attach); httpStream.flush(); httpStream.close(); }
/** * Updates an asset in Massive. The {@link Asset#get_id()} must return the correct ID for this * asset. Note that Massive will set some fields (such as last update date) so it is important to * switch to the returned object after calling this method. * * @param asset The asset to add, it will not be modified by this method * @return The asset with information added by Massive * @throws IOException * @throws RequestFailureException */ public Asset updateAsset(Asset asset) throws IOException, BadVersionException, RequestFailureException { HttpURLConnection connection = createHttpURLConnectionToMassive("/assets/" + asset.get_id()); connection.setRequestMethod("PUT"); connection.setRequestProperty("Content-Type", "application/json"); connection.setDoOutput(true); JSONAssetConverter.writeValue(connection.getOutputStream(), asset); /* * Force the PUT to take place by getting the response code and making sure it is ok */ testResponseCode(connection); /* * PUTs don't return an (they just return "1" - not sure what that * means) so go and grab it so we have an updated one with the right * last update date in case they use it for optimistic locking */ return getAsset(asset.get_id()); }