ContentRest extends BaseRest { protected Response addContentPropertiesToResponse(ResponseBuilder response, Map<String, String> properties) { if (properties != null) { properties.remove(HttpHeaders.ACCEPT_RANGES); response.header(HttpHeaders.ACCEPT_RANGES, HttpHeaders.ACCEPT_RANGES_BYTES); String contentMimetype = properties.remove(StorageProvider.PROPERTIES_CONTENT_MIMETYPE); String contentType = properties.remove(HttpHeaders.CONTENT_TYPE); if (null != contentMimetype && validMimetype(contentMimetype)) { response.header(HttpHeaders.CONTENT_TYPE, contentMimetype); } else if (null != contentType && validMimetype(contentType)) { response.header(HttpHeaders.CONTENT_TYPE, contentType); } else { response.header(HttpHeaders.CONTENT_TYPE, DEFAULT_MIME); } String contentSize = properties.remove(StorageProvider.PROPERTIES_CONTENT_SIZE); String contentLength = properties.remove(HttpHeaders.CONTENT_LENGTH); if (null != contentSize) { response.header(HttpHeaders.CONTENT_LENGTH, contentSize); } else if (null != contentLength) { response.header(HttpHeaders.CONTENT_LENGTH, contentLength); } String contentModified = properties.remove(StorageProvider.PROPERTIES_CONTENT_MODIFIED); String lastModified = properties.remove(HttpHeaders.LAST_MODIFIED); if (null != contentModified) { response.header(HttpHeaders.LAST_MODIFIED, contentModified); } else if (null != lastModified) { response.header(HttpHeaders.LAST_MODIFIED, lastModified); } String contentChecksum = properties.remove(StorageProvider.PROPERTIES_CONTENT_CHECKSUM); String contentMdFive = properties.remove(StorageProvider.PROPERTIES_CONTENT_MD5); String contentMd5 = properties.remove(HttpHeaders.CONTENT_MD5); String etag = properties.remove(HttpHeaders.ETAG); if (null != contentChecksum) { response.header(HttpHeaders.CONTENT_MD5, contentChecksum); response.header(HttpHeaders.ETAG, contentChecksum); } else if (null != contentMdFive) { response.header(HttpHeaders.CONTENT_MD5, contentMdFive); response.header(HttpHeaders.ETAG, contentMdFive); } else if (null != contentMd5) { response.header(HttpHeaders.CONTENT_MD5, contentMd5); response.header(HttpHeaders.ETAG, contentMd5); } else if (null != etag) { response.header(HttpHeaders.CONTENT_MD5, etag); response.header(HttpHeaders.ETAG, etag); } Iterator<String> propertiesNames = properties.keySet().iterator(); while (propertiesNames.hasNext()) { String propertiesName = propertiesNames.next(); String propertiesValue = properties.get(propertiesName); if (propertiesName.equals(HttpHeaders.DATE) || propertiesName.equals(HttpHeaders.CONNECTION)) { } else if (propertiesName.equals(HttpHeaders.ACCEPT_RANGES) || propertiesName.equals(HttpHeaders.AGE) || propertiesName.equals(HttpHeaders.CACHE_CONTROL) || propertiesName.equals(HttpHeaders.CONTENT_ENCODING) || propertiesName.equals(HttpHeaders.CONTENT_LANGUAGE) || propertiesName.equals(HttpHeaders.CONTENT_LOCATION) || propertiesName.equals(HttpHeaders.CONTENT_RANGE) || propertiesName.equals(HttpHeaders.EXPIRES) || propertiesName.equals(HttpHeaders.LOCATION) || propertiesName.equals(HttpHeaders.PRAGMA) || propertiesName.equals(HttpHeaders.RETRY_AFTER) || propertiesName.equals(HttpHeaders.SERVER) || propertiesName.equals(HttpHeaders.TRANSFER_ENCODING) || propertiesName.equals(HttpHeaders.UPGRADE) || propertiesName.equals(HttpHeaders.WARNING)) { response.header(propertiesName, propertiesValue); } else { response.header(HEADER_PREFIX + propertiesName, propertiesValue); } } } return response.build(); } @Autowired  ContentRest(ContentResource contentResource, RestUtil restUtil); @GET Response getContent(@PathParam("spaceID") String spaceID,
                               @PathParam("contentID") String contentID,
                               @QueryParam("storeID") String storeID,
                               @QueryParam("attachment") boolean attachment,
                               @HeaderParam(RANGE_HEADER) String range); @HEAD Response getContentProperties(@PathParam("spaceID") String spaceID,
                                         @PathParam("contentID") String contentID,
                                         @QueryParam("storeID") String storeID); @POST Response updateContentProperties(@PathParam("spaceID") String spaceID,
                                            @PathParam("contentID") String contentID,
                                            @QueryParam("storeID") String storeID); @PUT Response putContent(@PathParam("spaceID") String spaceID,
                               @PathParam("contentID") String contentID,
                               @QueryParam("storeID") String storeID,
                               @HeaderParam(COPY_SOURCE_HEADER) String copySource,
                               @HeaderParam(COPY_SOURCE_STORE_HEADER) String sourceStoreID); @DELETE Response deleteContent(@PathParam("spaceID") String spaceID,
                                  @PathParam("contentID") String contentID,
                                  @QueryParam("storeID") String storeID);  }
@Test public void testAddContentPropertiesToResponse() { Response response; replayMocks(); contentRest = new ContentRest(null, null); String contentType = "text/contenttype"; String contentMimetype = "text/contentmimetype"; String contentLength = "12345"; String contentSize = "67890"; String lastModified = "lastmodified"; String contentModified = "contentmodified"; String etag = "etag"; String contentmdfive = "contentmdfive"; String contentmd5 = "conentmd5"; String contentChecksum = "contentchecksum"; Map<String, String> props = new HashMap<>(); props.put(HttpHeaders.CONTENT_TYPE, contentType); response = contentRest.addContentPropertiesToResponse(Response.ok(), props); assertEquals(contentType, getHeader(response, HttpHeaders.CONTENT_TYPE)); props.put(StorageProvider.PROPERTIES_CONTENT_MIMETYPE, contentMimetype); response = contentRest.addContentPropertiesToResponse(Response.ok(), props); assertEquals(contentMimetype, getHeader(response, HttpHeaders.CONTENT_TYPE)); props.put(HttpHeaders.CONTENT_LENGTH, contentLength); response = contentRest.addContentPropertiesToResponse(Response.ok(), props); assertEquals(contentLength, getHeader(response, HttpHeaders.CONTENT_LENGTH)); props.put(StorageProvider.PROPERTIES_CONTENT_SIZE, contentSize); response = contentRest.addContentPropertiesToResponse(Response.ok(), props); assertEquals(contentSize, getHeader(response, HttpHeaders.CONTENT_LENGTH)); props.put(HttpHeaders.LAST_MODIFIED, lastModified); response = contentRest.addContentPropertiesToResponse(Response.ok(), props); assertEquals(lastModified, getHeader(response, HttpHeaders.LAST_MODIFIED)); props.put(StorageProvider.PROPERTIES_CONTENT_MODIFIED, contentModified); response = contentRest.addContentPropertiesToResponse(Response.ok(), props); assertEquals(contentModified, getHeader(response, HttpHeaders.LAST_MODIFIED)); props.put(HttpHeaders.ETAG, etag); response = contentRest.addContentPropertiesToResponse(Response.ok(), props); assertEquals(etag, getHeader(response, HttpHeaders.ETAG)); assertEquals(etag, getHeader(response, HttpHeaders.CONTENT_MD5)); props.put(HttpHeaders.CONTENT_MD5, contentmd5); response = contentRest.addContentPropertiesToResponse(Response.ok(), props); assertEquals(contentmd5, getHeader(response, HttpHeaders.ETAG)); assertEquals(contentmd5, getHeader(response, HttpHeaders.CONTENT_MD5)); props.put(StorageProvider.PROPERTIES_CONTENT_MD5, contentmdfive); response = contentRest.addContentPropertiesToResponse(Response.ok(), props); assertEquals(contentmdfive, getHeader(response, HttpHeaders.ETAG)); assertEquals(contentmdfive, getHeader(response, HttpHeaders.CONTENT_MD5)); props.put(StorageProvider.PROPERTIES_CONTENT_CHECKSUM, contentChecksum); response = contentRest.addContentPropertiesToResponse(Response.ok(), props); assertEquals(contentChecksum, getHeader(response, HttpHeaders.ETAG)); assertEquals(contentChecksum, getHeader(response, HttpHeaders.CONTENT_MD5)); props.put(HttpHeaders.DATE, "date"); props.put(HttpHeaders.CONNECTION, "connection"); response = contentRest.addContentPropertiesToResponse(Response.ok(), props); assertHeaderMissing(response, HttpHeaders.DATE); assertHeaderMissing(response, HttpHeaders.CONNECTION); String age = "age"; String cacheControl = "cachecontrol"; String contentEncoding = "contentencoding"; String contentLanguage = "contentlanguage"; String contentLocation = "contentlocation"; String contentRange = "contentrange"; String expires = "expires"; String location = "location"; String pragma = "pragma"; String retryAfter = "retryafter"; String server = "server"; String transferEncoding = "transferEncoding"; String upgrade = "upgrade"; String warning = "warning"; props.put(HttpHeaders.AGE, age); props.put(HttpHeaders.CACHE_CONTROL, cacheControl); props.put(HttpHeaders.CONTENT_ENCODING, contentEncoding); props.put(HttpHeaders.CONTENT_LANGUAGE, contentLanguage); props.put(HttpHeaders.CONTENT_LOCATION, contentLocation); props.put(HttpHeaders.CONTENT_RANGE, contentRange); props.put(HttpHeaders.EXPIRES, expires); props.put(HttpHeaders.LOCATION, location); props.put(HttpHeaders.PRAGMA, pragma); props.put(HttpHeaders.RETRY_AFTER, retryAfter); props.put(HttpHeaders.SERVER, server); props.put(HttpHeaders.TRANSFER_ENCODING, transferEncoding); props.put(HttpHeaders.UPGRADE, upgrade); props.put(HttpHeaders.WARNING, warning); response = contentRest.addContentPropertiesToResponse(Response.ok(), props); assertEquals(age, getHeader(response, HttpHeaders.AGE)); assertEquals(cacheControl, getHeader(response, HttpHeaders.CACHE_CONTROL)); assertEquals(contentEncoding, getHeader(response, HttpHeaders.CONTENT_ENCODING)); assertEquals(contentLanguage, getHeader(response, HttpHeaders.CONTENT_LANGUAGE)); assertEquals(contentLocation, getHeader(response, HttpHeaders.CONTENT_LOCATION)); assertEquals(contentRange, getHeader(response, HttpHeaders.CONTENT_RANGE)); assertEquals(expires, getHeader(response, HttpHeaders.EXPIRES)); assertEquals(location, getHeader(response, HttpHeaders.LOCATION)); assertEquals(pragma, getHeader(response, HttpHeaders.PRAGMA)); assertEquals(retryAfter, getHeader(response, HttpHeaders.RETRY_AFTER)); assertEquals(server, getHeader(response, HttpHeaders.SERVER)); assertEquals(transferEncoding, getHeader(response, HttpHeaders.TRANSFER_ENCODING)); assertEquals(upgrade, getHeader(response, HttpHeaders.UPGRADE)); assertEquals(warning, getHeader(response, HttpHeaders.WARNING)); String customNameOne = "custom-name-one"; String customValueOne = "Custom Value One!!"; String customNameTwo = "custom-name-two"; String customValueTwo = "CuSTom Value 2"; props.put(customNameOne, customValueOne); props.put(customNameTwo, customValueTwo); response = contentRest.addContentPropertiesToResponse(Response.ok(), props); assertEquals(customValueOne, getHeader(response, ContentRest.HEADER_PREFIX + customNameOne)); assertEquals(customValueTwo, getHeader(response, ContentRest.HEADER_PREFIX + customNameTwo)); assertHeaderMissing(response, StorageProvider.PROPERTIES_CONTENT_MIMETYPE); assertHeaderMissing(response, StorageProvider.PROPERTIES_CONTENT_SIZE); assertHeaderMissing(response, StorageProvider.PROPERTIES_CONTENT_MODIFIED); assertHeaderMissing(response, StorageProvider.PROPERTIES_CONTENT_CHECKSUM); for (String headerName : response.getMetadata().keySet()) { assertEquals(1, response.getMetadata().get(headerName).size()); } }