/** * Constructor. If a positive "timeToLive" parameter is given, then the * expiration date is set accordingly. If "timeToLive" is equal to zero, * then the expiration date is set to the current date, meaning that it will * immediately expire on the client. If -1 is given, then no expiration date * is set. * * @param file * The represented file. * @param mediaType * The representation's media type. * @param timeToLive * The time to live before it expires (in seconds). */ public FileRepresentation(File file, MediaType mediaType, int timeToLive) { super(mediaType); this.file = file; setModificationDate(new Date(file.lastModified())); if (timeToLive == 0) { setExpirationDate(null); } else if (timeToLive > 0) { setExpirationDate(new Date(System.currentTimeMillis() + (1000L * timeToLive))); } setMediaType(mediaType); Disposition disposition = new Disposition(); disposition.setFilename(file.getName()); this.setDisposition(disposition); }
@Override public ResponseEntity uploadOctetStream(String urn, java.io.File file, MediaType mediaType) throws ServiceException { ResponseEntity response; FileRepresentation fileRepresentation = new FileRepresentation(file, mediaType); fileRepresentation.setMediaType(MediaType.APPLICATION_OCTET_STREAM); ClientResource service = createClient(FileEndpoints.uploadContentsAsOctetStream(urn)); service.accept(MediaType.APPLICATION_JSON); try { Representation result = service.post(fileRepresentation); JsonRepresentation jsonRepresentation = new JsonRepresentation(result); JSONObject jsonResult = jsonRepresentation.getJsonObject(); response = JsonUtil.fromJson(jsonResult, ResponseEntity.class); if (service.getStatus().equals(Status.SUCCESS_OK)) { LOGGER.info("Successfully uploaded file to path {}", FileEndpoints.uploadContentsAsMultipart(urn)); } else { LOGGER.error("Unexpected HTTP status code returned: {}", service.getStatus().getCode()); throw new ServiceException(response); } } catch (JSONException | IOException e) { LOGGER.error("Unexpected Exception", e); throw new ServiceException(e); } return response; }