/** * 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; }
/** * This will obtain assets from Massive using the supplied filters. The map can contain <code>null * </code> or empty collections of values, in which case they will not be used in the filter. * * @param filters A map of attributes to filter on mapped to the values to use * @return The filtered assets * @throws IOException * @throws RequestFailureException */ public Collection<Asset> getFilteredAssets(Map<FilterableAttribute, Collection<String>> filters) throws IOException, RequestFailureException { // Were any filters defined? if (filters == null || allFiltersAreEmpty(filters)) { return getAllAssets(); } // Build up a filter string Collection<String> filterStrings = new HashSet<String>(); for (Map.Entry<FilterableAttribute, Collection<String>> filter : filters.entrySet()) { Collection<String> values = filter.getValue(); if (values != null && !values.isEmpty()) { filterStrings.add(createListFilter(filter.getKey(), values)); } } StringBuilder filterString = new StringBuilder("?"); boolean isFirst = true; for (String filter : filterStrings) { if (isFirst) { isFirst = false; } else { filterString.append("&"); } filterString.append(filter); } // Now do the filtered call into massive HttpURLConnection connection = createHttpURLConnectionToMassive("/assets" + filterString.toString()); connection.setRequestMethod("GET"); testResponseCode(connection); return JSONAssetConverter.readValues(connection.getInputStream()); }
/** * Gets a single asset * * @param assetId The ID of the asset to obtain * @return The Asset * @throws IOException * @throws RequestFailureException */ public Asset getAsset(String assetId) throws IOException, BadVersionException, RequestFailureException { HttpURLConnection connection = createHttpURLConnectionToMassive("/assets/" + assetId); connection.setRequestMethod("GET"); testResponseCode(connection); return JSONAssetConverter.readValue(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; }
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); }
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()); }
/** * Implements the find API for featureManager find * * <p>NOTE: TODO at the moment this only works when called against an unauthenticated Client due * to a problem with how the stores are defined (the company values are defined incorrectly). * * @param searchString * @param typeLabel * @param appliesToFilterInfo * @return * @throws IOException * @throws RequestFailureException */ public List<Asset> findAssets(String searchString, TypeLabel assetType, Visibility visible) throws IOException, RequestFailureException { String encodedSearchString = URLEncoder.encode(searchString, "UTF-8"); StringBuffer url = new StringBuffer("/assets?q=" + encodedSearchString); url.append("&wlpInformation.typeLabel=" + assetType.getValue()); // visibility is currently not indexed ... when it is this line can be uncommented. Until then // it will kill all the results // url.append("&wlpInformation.visibility=" + visible.toString()); // Call massive to run the query HttpURLConnection connection = createHttpURLConnectionToMassive(url.toString()); connection.setRequestMethod("GET"); testResponseCode(connection); InputStream is = connection.getInputStream(); // take the returned input stream and convert it to assets List<Asset> assets = JSONAssetConverter.readValues(is); return assets; }
/** * This method will issue a GET to all of the assets in massive * * @return A list of all of the assets in Massive * @throws IOException * @throws RequestFailureException */ public List<Asset> getAllAssets() throws IOException, RequestFailureException { HttpURLConnection connection = createHttpURLConnectionToMassive("/assets"); connection.setRequestMethod("GET"); testResponseCode(connection); return JSONAssetConverter.readValues(connection.getInputStream()); }