List of usage examples for org.springframework.mock.web MockHttpServletRequest setContentType
public void setContentType(@Nullable String contentType)
From source file:org.geoserver.rest.catalog.CoverageStoreFileUploadTest.java
@Test public void testHarvestImageMosaic() throws Exception { // Upload of the Mosaic via REST URL zip = MockData.class.getResource("watertemp.zip"); InputStream is = null;//from w w w .j a v a 2s . c o m byte[] bytes; try { is = zip.openStream(); bytes = IOUtils.toByteArray(is); } finally { IOUtils.closeQuietly(is); } MockHttpServletResponse response = putAsServletResponse( RestBaseController.ROOT_PATH + "/workspaces/gs/coveragestores/watertemp2/file.imagemosaic", bytes, "application/zip"); assertEquals(201, response.getStatus()); // check the response contents String content = response.getContentAsString(); Document d = dom(new ByteArrayInputStream(content.getBytes())); XMLAssert.assertXpathEvaluatesTo("watertemp2", "//coverageStore/name", d); XMLAssert.assertXpathEvaluatesTo("ImageMosaic", "//coverageStore/type", d); // check the coverage is actually there CoverageStoreInfo storeInfo = getCatalog().getCoverageStoreByName("watertemp2"); assertNotNull(storeInfo); CoverageInfo ci = getCatalog().getCoverageByName("watertemp2"); assertNotNull(ci); assertEquals(storeInfo, ci.getStore()); // Harvesting of the Mosaic URL zipHarvest = MockData.class.getResource("harvesting.zip"); // Extract a Byte array from the zip file is = null; try { is = zipHarvest.openStream(); bytes = IOUtils.toByteArray(is); } finally { IOUtils.closeQuietly(is); } // Create the POST request MockHttpServletRequest request = createRequest( RestBaseController.ROOT_PATH + "/workspaces/gs/coveragestores/watertemp2/file.imagemosaic"); request.setMethod("POST"); request.setContentType("application/zip"); request.setContent(bytes); request.addHeader("Content-type", "application/zip"); // Get The response dispatch(request); // Get the Mosaic Reader GridCoverageReader reader = storeInfo.getGridCoverageReader(null, GeoTools.getDefaultHints()); // Test if all the TIME DOMAINS are present String[] metadataNames = reader.getMetadataNames(); assertNotNull(metadataNames); assertEquals("true", reader.getMetadataValue("HAS_TIME_DOMAIN")); assertEquals("2008-10-31T00:00:00.000Z,2008-11-01T00:00:00.000Z,2008-11-02T00:00:00.000Z", reader.getMetadataValue(metadataNames[0])); }
From source file:org.geoserver.rest.catalog.CoverageStoreFileUploadTest.java
@Test public void testHarvestNotAllowedOnSimpleCoverageStore() throws Exception { // add bluemarble getTestData().addDefaultRasterLayer(SystemTestData.TASMANIA_BM, getCatalog()); // Harvesting of the Mosaic URL zipHarvest = MockData.class.getResource("harvesting.zip"); // Extract a Byte array from the zip file InputStream is = null;/*w w w . j a v a 2 s.c o m*/ byte[] bytes; try { is = zipHarvest.openStream(); bytes = IOUtils.toByteArray(is); } finally { IOUtils.closeQuietly(is); } // Create the POST request MockHttpServletRequest request = createRequest( RestBaseController.ROOT_PATH + "/workspaces/wcs/coveragestores/BlueMarble"); request.setMethod("POST"); request.setContentType("application/zip"); request.setContent(bytes); request.addHeader("Content-type", "application/zip"); // Get The response MockHttpServletResponse response = dispatch(request); // not allowed assertEquals(405, response.getStatus()); }
From source file:org.geoserver.rest.catalog.CoverageStoreFileUploadTest.java
@Test public void testHarvestImageMosaicWithDirectory() throws Exception { // Upload of the Mosaic via REST URL zip = MockData.class.getResource("watertemp.zip"); InputStream is = null;/* ww w . j a va 2s. co m*/ byte[] bytes; try { is = zip.openStream(); bytes = IOUtils.toByteArray(is); } finally { IOUtils.closeQuietly(is); } MockHttpServletResponse response = putAsServletResponse( RestBaseController.ROOT_PATH + "/workspaces/gs/coveragestores/watertemp3/file.imagemosaic", bytes, "application/zip"); assertEquals(201, response.getStatus()); // check the response contents String content = response.getContentAsString(); Document d = dom(new ByteArrayInputStream(content.getBytes())); XMLAssert.assertXpathEvaluatesTo("watertemp3", "//coverageStore/name", d); XMLAssert.assertXpathEvaluatesTo("ImageMosaic", "//coverageStore/type", d); // check the coverage is actually there CoverageStoreInfo storeInfo = getCatalog().getCoverageStoreByName("watertemp3"); assertNotNull(storeInfo); CoverageInfo ci = getCatalog().getCoverageByName("watertemp3"); assertNotNull(ci); assertEquals(storeInfo, ci.getStore()); // Harvesting of the Mosaic URL zipHarvest = MockData.class.getResource("harvesting.zip"); Resource newZip = Files.asResource(new File("./target/harvesting2.zip")); // Copy the content of the first zip to the second IOUtils.copyStream(zipHarvest.openStream(), newZip.out(), true, true); Resource outputDirectory = Files.asResource(new File("./target/harvesting")); RESTUtils.unzipFile(newZip, outputDirectory); // Create the POST request MockHttpServletRequest request = createRequest( RestBaseController.ROOT_PATH + "/workspaces/gs/coveragestores/watertemp3/external.imagemosaic"); request.setMethod("POST"); request.setContentType("text/plain"); request.setContent(("file:///" + outputDirectory.dir().getAbsolutePath()).getBytes("UTF-8")); request.addHeader("Content-type", "text/plain"); // Get The response dispatch(request); // Get the Mosaic Reader GridCoverageReader reader = storeInfo.getGridCoverageReader(null, GeoTools.getDefaultHints()); // Test if all the TIME DOMAINS are present String[] metadataNames = reader.getMetadataNames(); assertNotNull(metadataNames); assertEquals("true", reader.getMetadataValue("HAS_TIME_DOMAIN")); assertEquals("2008-10-31T00:00:00.000Z,2008-11-01T00:00:00.000Z,2008-11-02T00:00:00.000Z", reader.getMetadataValue(metadataNames[0])); // Removal of the temporary directory outputDirectory.delete(); }
From source file:org.geoserver.rest.catalog.CoverageStoreFileUploadTest.java
@Test public void testHarvestExternalImageMosaic() throws Exception { // Check if an already existing directory called "mosaic" is present URL resource = getClass().getResource("test-data/mosaic"); if (resource != null) { File oldDir = DataUtilities.urlToFile(resource); if (oldDir.exists()) { FileUtils.deleteDirectory(oldDir); }//from ww w .j a v a 2 s . c om } // reading of the mosaic directory Resource mosaic = readMosaic(); // Creation of the builder for building a new CoverageStore CatalogBuilder builder = new CatalogBuilder(getCatalog()); // Definition of the workspace associated to the coverage WorkspaceInfo ws = getCatalog().getWorkspaceByName("gs"); // Creation of a CoverageStore CoverageStoreInfo store = builder.buildCoverageStore("watertemp4"); store.setURL(DataUtilities.fileToURL(Resources.find(mosaic)).toExternalForm()); store.setWorkspace(ws); ImageMosaicFormat imageMosaicFormat = new ImageMosaicFormat(); store.setType((imageMosaicFormat.getName())); // Addition to the catalog getCatalog().add(store); builder.setStore(store); // Input reader used for reading the mosaic folder GridCoverage2DReader reader = null; // Reader used for checking if the mosaic has been configured correctly StructuredGridCoverage2DReader reader2 = null; try { // Selection of the reader to use for the mosaic reader = imageMosaicFormat.getReader(DataUtilities.fileToURL(Resources.find(mosaic))); // configure the coverage configureCoverageInfo(builder, store, reader); // check the coverage is actually there CoverageStoreInfo storeInfo = getCatalog().getCoverageStoreByName("watertemp4"); assertNotNull(storeInfo); CoverageInfo ci = getCatalog().getCoverageByName("mosaic"); assertNotNull(ci); assertEquals(storeInfo, ci.getStore()); // Harvesting of the Mosaic URL zipHarvest = MockData.class.getResource("harvesting.zip"); // Extract a Byte array from the zip file InputStream is = null; byte[] bytes; try { is = zipHarvest.openStream(); bytes = IOUtils.toByteArray(is); } finally { IOUtils.closeQuietly(is); } // Create the POST request MockHttpServletRequest request = createRequest( RestBaseController.ROOT_PATH + "/workspaces/gs/coveragestores/watertemp4/file.imagemosaic"); request.setMethod("POST"); request.setContentType("application/zip"); request.setContent(bytes); request.addHeader("Content-type", "application/zip"); // Get The response MockHttpServletResponse response = dispatch(request); // Get the Mosaic Reader reader2 = (StructuredGridCoverage2DReader) storeInfo.getGridCoverageReader(null, GeoTools.getDefaultHints()); // Test if all the TIME DOMAINS are present String[] metadataNames = reader2.getMetadataNames(); assertNotNull(metadataNames); assertEquals("true", reader2.getMetadataValue("HAS_TIME_DOMAIN")); assertEquals("2008-10-31T00:00:00.000Z,2008-11-01T00:00:00.000Z,2008-11-02T00:00:00.000Z", reader2.getMetadataValue(metadataNames[0])); // Removal of all the data associated to the mosaic reader2.delete(true); } finally { // Reader disposal if (reader != null) { try { reader.dispose(); } catch (Throwable t) { // Does nothing } } if (reader2 != null) { try { reader2.dispose(); } catch (Throwable t) { // Does nothing } } } }
From source file:org.geoserver.rest.catalog.CoverageStoreFileUploadTest.java
private StructuredGridCoverage2DReader uploadGeotiffAndCheck(CoverageStoreInfo storeInfo, byte[] bytes, String filename) throws Exception { StructuredGridCoverage2DReader reader2; // Create the POST request MockHttpServletRequest request = createRequest(RestBaseController.ROOT_PATH + "/workspaces/gs/coveragestores/watertemp5/file.imagemosaic?filename=" + filename); request.setMethod("POST"); request.setContentType("image/tiff"); request.setContent(bytes);//from ww w. ja v a 2 s .co m request.addHeader("Content-type", "image/tiff"); // Get The response assertEquals(202, dispatch(request).getStatus()); // Get the Mosaic Reader reader2 = (StructuredGridCoverage2DReader) storeInfo.getGridCoverageReader(null, GeoTools.getDefaultHints()); // Test if all the TIME DOMAINS are present String[] metadataNames = reader2.getMetadataNames(); assertNotNull(metadataNames); assertEquals("true", reader2.getMetadataValue("HAS_TIME_DOMAIN")); assertEquals("2008-10-31T00:00:00.000Z,2008-11-01T00:00:00.000Z,2008-11-02T00:00:00.000Z", reader2.getMetadataValue(metadataNames[0])); return reader2; }
From source file:org.geoserver.rest.catalog.StyleControllerTest.java
/** * TODO I had to put this here BECAUSE:/*from w w w . j av a 2 s. c o m*/ * * - The testPutSLDPackage test uses a *.zip URL * - BUT, put style does not support ZIP responses * - Spring interprets the .zip extension on the path as being a request for a zip response * - This fails, because there is no actual handler for a zip response on a style endpoint * - Unfortunately Spring only considers one of the Accept header or the path * - So the handler is never found * * this leaves us with a few options * * 1) Configure spring to prefer the accept header over the path. This would: * * - Force future clients who depended on put/posting to zip endpoints to make sure their * Accept header is correct. * - Maybe more importantly it could potentially break other end points that depend on preferring * the path extension. * * 2) Continue letting Spring prefer the path (which is really the right behavior for a REST api) * * - Future clients would not be able to use an endpoint like .zip * - But this is more REST-y * * 3) Write our own content negotiation strategy that allows for both. * * - This is a pain in the ass. * - Potentially difficult to recreate all default behavior + behavior needed to fix this test * case * * @param path * @param body * @param contentType * @return * @throws Exception */ protected MockHttpServletResponse putAsServletResponse(String path, byte[] body, String contentType, String accepts) throws Exception { MockHttpServletRequest request = createRequest(path); request.setMethod("PUT"); request.setContentType(contentType); request.setContent(body); request.addHeader("Accept", accepts); request.addHeader("Content-type", contentType); return dispatch(request); }
From source file:org.geoserver.restupload.ResumableUploadTest.java
@Test public void testUploadFull() throws Exception { String uploadId = sendPostRequest(); File uploadedFile = getTempPath(uploadId); assertTrue(uploadedFile.exists());//from w w w. j a va2 s .c o m MockHttpServletRequest request = createRequest("/rest/resumableupload/" + uploadId); request.setMethod("PUT"); request.setContentType("application/octet-stream"); byte[] bigFile = generateFileAsBytes(); request.setContent(bigFile); request.addHeader("Content-type", "application/octet-stream"); request.addHeader("Content-Length", String.valueOf(bigFile.length)); MockHttpServletResponse response = dispatch(request); assertEquals(Status.SUCCESS_OK.getCode(), response.getStatus()); assertFalse(uploadedFile.exists()); File destinationFile = new File(FilenameUtils.concat(root, fileName.replaceAll("^/", ""))); assertTrue(destinationFile.exists()); assertEquals(bigFile.length, destinationFile.length()); // Check uploaded file byte by byte boolean checkBytes = Arrays.equals(bigFile, toBytes(new FileInputStream(destinationFile))); assertTrue(checkBytes); // Check response content String restUrl = response.getContentAsString(); assertEquals(fileName.replaceAll("^/", ""), restUrl); }
From source file:org.geoserver.restupload.ResumableUploadTest.java
@Test public void testPartialUpload() throws Exception { String uploadId = sendPostRequest(); MockHttpServletRequest request = createRequest("/rest/resumableupload/" + uploadId); request.setMethod("PUT"); request.setContentType("application/octet-stream"); byte[] bigFile = generateFileAsBytes(); byte[] partialFile = ArrayUtils.subarray(bigFile, 0, (int) partialSize); request.setContent(partialFile);//from ww w. j a v a 2 s . c o m request.addHeader("Content-type", "application/octet-stream"); request.addHeader("Content-Length", String.valueOf(bigFile.length)); MockHttpServletResponse response = dispatch(request); assertEquals(ResumableUploadCatalogResource.RESUME_INCOMPLETE.getCode(), response.getStatus()); assertEquals(null, response.getHeader("Content-Length")); assertEquals("0-" + (partialSize - 1), response.getHeader("Range")); File uploadedFile = getTempPath(uploadId); assertTrue(uploadedFile.exists()); assertEquals(partialSize, uploadedFile.length()); boolean checkBytes = Arrays.equals(partialFile, toBytes(new FileInputStream(uploadedFile))); assertTrue(checkBytes); }
From source file:org.geoserver.restupload.ResumableUploadTest.java
@Test public void testUploadPartialResume() throws Exception { String uploadId = sendPostRequest(); byte[] bigFile = generateFileAsBytes(); byte[] partialFile1 = ArrayUtils.subarray(bigFile, 0, (int) partialSize); // First upload MockHttpServletRequest request = createRequest("/rest/resumableupload/" + uploadId); request.setMethod("PUT"); request.setContentType("application/octet-stream"); request.setContent(partialFile1);// w ww . j a va2s . c om request.addHeader("Content-type", "application/octet-stream"); request.addHeader("Content-Length", String.valueOf(bigFile.length)); dispatch(request); // Resume upload request = createRequest("/rest/resumableupload/" + uploadId); request.setMethod("PUT"); request.setContentType("application/octet-stream"); byte[] partialFile2 = ArrayUtils.subarray(bigFile, (int) partialSize, (int) partialSize * 2); request.setContent(partialFile2); request.addHeader("Content-type", "application/octet-stream"); request.addHeader("Content-Length", String.valueOf(partialFile2.length)); request.addHeader("Content-Range", "bytes " + partialSize + "-" + partialSize * 2 + "/" + bigFile.length); MockHttpServletResponse response = dispatch(request); assertEquals(ResumableUploadCatalogResource.RESUME_INCOMPLETE.getCode(), response.getStatus()); assertEquals(null, response.getHeader("Content-Length")); assertEquals("0-" + (partialSize * 2 - 1), response.getHeader("Range")); File uploadedFile = getTempPath(uploadId); assertTrue(uploadedFile.exists()); assertEquals(partialSize * 2, uploadedFile.length()); // Check uploaded file byte by byte boolean checkBytes = Arrays.equals(ArrayUtils.addAll(partialFile1, partialFile2), toBytes(new FileInputStream(uploadedFile))); assertTrue(checkBytes); }
From source file:org.geoserver.restupload.ResumableUploadTest.java
@Test public void testUploadFullResume() throws Exception { String uploadId = sendPostRequest(); byte[] bigFile = generateFileAsBytes(); byte[] partialFile1 = ArrayUtils.subarray(bigFile, 0, (int) partialSize); // First upload MockHttpServletRequest request = createRequest("/rest/resumableupload/" + uploadId); request.setMethod("PUT"); request.setContentType("application/octet-stream"); request.setContent(partialFile1);/*from www . ja v a 2s . com*/ request.addHeader("Content-type", "application/octet-stream"); request.addHeader("Content-Length", String.valueOf(bigFile.length)); dispatch(request); // Resume upload request = createRequest("/rest/resumableupload/" + uploadId); request.setMethod("PUT"); request.setContentType("application/octet-stream"); byte[] partialFile2 = ArrayUtils.subarray(bigFile, (int) partialSize, bigFile.length); request.setContent(partialFile2); request.addHeader("Content-type", "application/octet-stream"); request.addHeader("Content-Length", String.valueOf(partialFile2.length)); request.addHeader("Content-Range", "bytes " + partialSize + "-" + bigFile.length + "/" + bigFile.length); MockHttpServletResponse response = dispatch(request); assertEquals(Status.SUCCESS_OK.getCode(), response.getStatus()); File uploadedFile = getTempPath(uploadId); assertFalse(uploadedFile.exists()); File destinationFile = new File(FilenameUtils.concat(root, fileName.replaceAll("^/", ""))); assertTrue(destinationFile.exists()); assertEquals(bigFile.length, destinationFile.length()); // Check uploaded file byte by byte boolean checkBytes = Arrays.equals(bigFile, toBytes(new FileInputStream(destinationFile))); assertTrue(checkBytes); // Check response content String restUrl = response.getContentAsString(); assertEquals(fileName.replaceAll("^/", ""), restUrl); }