List of usage examples for java.net URI toURL
public URL toURL() throws MalformedURLException
From source file:nl.mpi.lamus.workspace.upload.implementation.LamusWorkspaceUploaderTest.java
@Test public void processOneUploadedMetadataFile_withTwoValidationIssues_Error() throws IOException, WorkspaceNodeNotFoundException, URISyntaxException, WorkspaceException, NodeNotFoundException, TypeCheckerException, Exception { final String filename = "someFile.cmdi"; final URI workspaceTopNodeArchiveURI = URI .create(handleProxyPlusPrefixWithSlash + UUID.randomUUID().toString()); final File workspaceTopNodeArchiveFile = new File("/archive/some/node.cmdi"); final File uploadedFile = new File(workspaceUploadDirectory, filename); final URI uploadedFileURI = uploadedFile.toURI(); final URI uploadedFileArchiveURI = URI .create(handleProxyPlusPrefixWithSlash + UUID.randomUUID().toString()); final URL uploadedFileURL = uploadedFileURI.toURL(); final WorkspaceNodeType fileType = WorkspaceNodeType.METADATA; final String fileMimetype = "text/x-cmdi-xml"; final WorkspaceNode uploadedNode = new LamusWorkspaceNode(workspaceID, null, null); uploadedNode.setName(filename);/*from w ww.j a v a2 s .c om*/ uploadedNode.setStatus(WorkspaceNodeStatus.UPLOADED); uploadedNode.setType(fileType); uploadedNode.setFormat(fileMimetype); uploadedNode.setWorkspaceURL(uploadedFileURL); uploadedNode.setArchiveURI(uploadedFileArchiveURI); final Collection<File> uploadedFiles = new ArrayList<>(); uploadedFiles.add(mockFile1); final Collection<MetadataValidationIssue> issues = new ArrayList<>(); issues.add(mockValidationIssue1); issues.add(mockValidationIssue2); final Collection<WorkspaceNode> uploadedNodes = new ArrayList<>(); final String assertionErrorMessage1 = "[CMDI Archive Restriction] the CMD profile of this record is not allowed in the archive."; final String assertionErrorMessage2 = "[CMDI Archive Restriction] Something completely different went wrong."; final String validationIssuesString = "Validation issue for file '" + filename + "' - " + MetadataValidationIssueSeverity.ERROR.toString() + ": " + assertionErrorMessage1 + ".\n" + "Validation issue for file '" + filename + "' - " + MetadataValidationIssueSeverity.ERROR.toString() + ": " + assertionErrorMessage2 + ".\n"; final MetadataValidationException expectedException = new MetadataValidationException( validationIssuesString, workspaceID, null); expectedException.addValidationIssues(issues); //only one file in the collection, so only one loop cycle context.checking(new Expectations() { { oneOf(mockWorkspaceDao).getWorkspace(workspaceID); will(returnValue(mockWorkspace)); oneOf(mockWorkspaceDao).getWorkspaceTopNode(workspaceID); will(returnValue(mockWorkspaceTopNode)); oneOf(mockWorkspaceTopNode).getArchiveURI(); will(returnValue(workspaceTopNodeArchiveURI)); oneOf(mockNodeDataRetriever).getNodeLocalFile(workspaceTopNodeArchiveURI); will(returnValue(workspaceTopNodeArchiveFile)); //loop allowing(mockFile1).getName(); will(returnValue(filename)); oneOf(mockFile1).toURI(); will(returnValue(uploadedFileURI)); oneOf(mockNodeDataRetriever).triggerResourceFileCheck(uploadedFileURL, filename); will(returnValue(mockTypecheckedResults)); oneOf(mockNodeDataRetriever).isCheckedResourceArchivable(with(same(mockTypecheckedResults)), with(same(workspaceTopNodeArchiveFile)), with(any(StringBuilder.class))); will(returnValue(Boolean.TRUE)); oneOf(mockMetadataAPI).getMetadataDocument(uploadedFileURL); will(returnValue(mockMetadataDocument)); oneOf(mockWorkspaceFileValidator).triggerSchemaValidationForFile(workspaceID, mockFile1); oneOf(mockWorkspaceFileValidator).triggerSchematronValidationForFile(workspaceID, mockFile1); will(throwException(expectedException)); oneOf(mockWorkspaceFileValidator) .validationIssuesToString(with(equivalentValidationIssueCollection(issues))); will(returnValue(validationIssuesString)); oneOf(mockWorkspaceFileValidator) .validationIssuesContainErrors(with(equivalentValidationIssueCollection(issues))); will(returnValue(Boolean.TRUE)); oneOf(mockArchiveFileLocationProvider).isFileInOrphansDirectory(mockFile1); will(returnValue(Boolean.FALSE)); oneOf(mockWorkspaceFileHandler).deleteFile(mockFile1); //still calls method to process links oneOf(mockWorkspaceUploadHelper).assureLinksInWorkspace(mockWorkspace, uploadedNodes); } }); Collection<ImportProblem> result = uploader.processUploadedFiles(workspaceID, uploadedFiles); assertNotNull("Collection with failed uploads should not be null", result); assertTrue("Collection with failed uploads should have one element", result.size() == 1); ImportProblem problem = result.iterator().next(); assertTrue("Upload problem different from expected", problem instanceof FileImportProblem); assertEquals("File added to the upload problem is different from expected", mockFile1, ((FileImportProblem) problem).getProblematicFile()); assertEquals("Reason for failure of file upload is different from expected", validationIssuesString.trim(), ((FileImportProblem) problem).getErrorMessage().trim()); }
From source file:nl.mpi.lamus.workspace.upload.implementation.LamusWorkspaceUploaderTest.java
@Test public void processOneUploadedMetadataFile_NoValidationIssues_InvalidSelfHandle() throws IOException, WorkspaceNodeNotFoundException, URISyntaxException, WorkspaceException, NodeNotFoundException, TypeCheckerException, Exception { final String filename = "someFile.cmdi"; final String documentName = "SomeFile"; final URI workspaceTopNodeArchiveURI = URI .create(handleProxyPlusPrefixWithSlash + UUID.randomUUID().toString()); final File workspaceTopNodeArchiveFile = new File("/archive/some/node.cmdi"); final File uploadedFile = new File(workspaceUploadDirectory, filename); final URI uploadedFileURI = uploadedFile.toURI(); final String uploadedFileRawHandle = UUID.randomUUID().toString(); final URI uploadedFileArchiveURI = URI.create("INVALID/" + uploadedFileRawHandle); final URL uploadedFileURL = uploadedFileURI.toURL(); final URI schemaLocation = URI.create("http://some/location/schema.xsd"); final WorkspaceNodeType fileNodeType = WorkspaceNodeType.METADATA; final String fileMimetype = "text/x-cmdi-xml"; final WorkspaceNode uploadedNode = new LamusWorkspaceNode(workspaceID, null, null); uploadedNode.setName(filename);//from ww w . jav a2 s.co m uploadedNode.setStatus(WorkspaceNodeStatus.UPLOADED); uploadedNode.setType(fileNodeType); uploadedNode.setFormat(fileMimetype); uploadedNode.setWorkspaceURL(uploadedFileURL); uploadedNode.setArchiveURI(uploadedFileArchiveURI); final Collection<File> uploadedFiles = new ArrayList<>(); uploadedFiles.add(mockFile1); final Collection<WorkspaceNode> uploadedNodes = new ArrayList<>(); uploadedNodes.add(uploadedNode); //only one file in the collection, so only one loop cycle final Collection<ImportProblem> failedLinks = new ArrayList<>(); context.checking(new Expectations() { { oneOf(mockWorkspaceDao).getWorkspace(workspaceID); will(returnValue(mockWorkspace)); oneOf(mockWorkspaceDao).getWorkspaceTopNode(workspaceID); will(returnValue(mockWorkspaceTopNode)); oneOf(mockWorkspaceTopNode).getArchiveURI(); will(returnValue(workspaceTopNodeArchiveURI)); oneOf(mockNodeDataRetriever).getNodeLocalFile(workspaceTopNodeArchiveURI); will(returnValue(workspaceTopNodeArchiveFile)); //loop allowing(mockFile1).getName(); will(returnValue(filename)); oneOf(mockFile1).toURI(); will(returnValue(uploadedFileURI)); oneOf(mockNodeDataRetriever).triggerResourceFileCheck(uploadedFileURL, filename); will(returnValue(mockTypecheckedResults)); oneOf(mockNodeDataRetriever).isCheckedResourceArchivable(with(same(mockTypecheckedResults)), with(same(workspaceTopNodeArchiveFile)), with(any(StringBuilder.class))); will(returnValue(Boolean.TRUE)); oneOf(mockMetadataAPI).getMetadataDocument(uploadedFileURL); will(returnValue(mockMetadataDocument)); oneOf(mockWorkspaceFileValidator).triggerSchemaValidationForFile(workspaceID, mockFile1); oneOf(mockWorkspaceFileValidator).triggerSchematronValidationForFile(workspaceID, mockFile1); oneOf(mockTypecheckedResults).getCheckedMimetype(); will(returnValue(fileMimetype)); oneOf(mockNodeUtil).convertMimetype(fileMimetype); will(returnValue(fileNodeType)); oneOf(mockMetadataApiBridge).getSelfHandleFromDocument(mockMetadataDocument); will(returnValue(uploadedFileArchiveURI)); oneOf(mockHandleParser).prepareAndValidateHandleWithHdlPrefix(uploadedFileArchiveURI); will(throwException(new IllegalArgumentException())); oneOf(mockMetadataApiBridge).removeSelfHandleAndSaveDocument(uploadedFileURL); oneOf(mockArchiveFileLocationProvider).isFileInOrphansDirectory(mockFile1); will(returnValue(Boolean.FALSE)); oneOf(mockMetadataDocument).getDocumentType(); will(returnValue(mockMetadataDocumentType)); oneOf(mockMetadataDocumentType).getSchemaLocation(); will(returnValue(schemaLocation)); oneOf(mockMetadataApiBridge).getDocumentNameForProfile(mockMetadataDocument, schemaLocation); will(returnValue(documentName)); oneOf(mockWorkspaceNodeFactory).getNewWorkspaceNodeFromFile(workspaceID, null, null, uploadedFileURL, schemaLocation, documentName, fileMimetype, fileNodeType, WorkspaceNodeStatus.UPLOADED, Boolean.FALSE); will(returnValue(uploadedNode)); oneOf(mockWorkspaceDao).addWorkspaceNode(uploadedNode); //check links oneOf(mockWorkspaceUploadHelper).assureLinksInWorkspace(mockWorkspace, uploadedNodes); will(returnValue(failedLinks)); } }); Collection<ImportProblem> result = uploader.processUploadedFiles(workspaceID, uploadedFiles); assertNotNull("Collection with failed uploads should not be null", result); assertTrue("Collection with failed uploads should be empty", result.isEmpty()); }
From source file:nl.mpi.lamus.workspace.upload.implementation.LamusWorkspaceUploaderTest.java
@Test public void processOneUploadedMetadataFile_NoValidationIssues() throws IOException, WorkspaceNodeNotFoundException, URISyntaxException, WorkspaceException, NodeNotFoundException, TypeCheckerException, Exception { final String filename = "someFile.cmdi"; final String documentName = "SomeFile"; final URI workspaceTopNodeArchiveURI = URI .create(handleProxyPlusPrefixWithSlash + UUID.randomUUID().toString()); final File workspaceTopNodeArchiveFile = new File("/archive/some/node.cmdi"); final File uploadedFile = new File(workspaceUploadDirectory, filename); final URI uploadedFileURI = uploadedFile.toURI(); final String uploadedFileRawHandle = UUID.randomUUID().toString(); final URI uploadedFileArchiveURI = URI.create(handlePrefixWithSlash + uploadedFileRawHandle); final URI completeFileArchiveURI = URI.create(handleProxyPlusPrefixWithSlash + uploadedFileRawHandle); final URL uploadedFileURL = uploadedFileURI.toURL(); final URI schemaLocation = URI.create("http://some/location/schema.xsd"); final WorkspaceNodeType fileNodeType = WorkspaceNodeType.METADATA; final String fileMimetype = "text/x-cmdi-xml"; final WorkspaceNode uploadedNode = new LamusWorkspaceNode(workspaceID, null, null); uploadedNode.setName(documentName);/* ww w .j a v a 2 s . co m*/ uploadedNode.setStatus(WorkspaceNodeStatus.UPLOADED); uploadedNode.setType(fileNodeType); uploadedNode.setFormat(fileMimetype); uploadedNode.setWorkspaceURL(uploadedFileURL); uploadedNode.setArchiveURI(uploadedFileArchiveURI); final Collection<File> uploadedFiles = new ArrayList<>(); uploadedFiles.add(mockFile1); final Collection<WorkspaceNode> uploadedNodes = new ArrayList<>(); uploadedNodes.add(uploadedNode); //only one file in the collection, so only one loop cycle final Collection<ImportProblem> failedLinks = new ArrayList<>(); context.checking(new Expectations() { { oneOf(mockWorkspaceDao).getWorkspace(workspaceID); will(returnValue(mockWorkspace)); oneOf(mockWorkspaceDao).getWorkspaceTopNode(workspaceID); will(returnValue(mockWorkspaceTopNode)); oneOf(mockWorkspaceTopNode).getArchiveURI(); will(returnValue(workspaceTopNodeArchiveURI)); oneOf(mockNodeDataRetriever).getNodeLocalFile(workspaceTopNodeArchiveURI); will(returnValue(workspaceTopNodeArchiveFile)); //loop allowing(mockFile1).getName(); will(returnValue(filename)); oneOf(mockFile1).toURI(); will(returnValue(uploadedFileURI)); oneOf(mockNodeDataRetriever).triggerResourceFileCheck(uploadedFileURL, filename); will(returnValue(mockTypecheckedResults)); oneOf(mockNodeDataRetriever).isCheckedResourceArchivable(with(same(mockTypecheckedResults)), with(same(workspaceTopNodeArchiveFile)), with(any(StringBuilder.class))); will(returnValue(Boolean.TRUE)); oneOf(mockMetadataAPI).getMetadataDocument(uploadedFileURL); will(returnValue(mockMetadataDocument)); oneOf(mockWorkspaceFileValidator).triggerSchemaValidationForFile(workspaceID, mockFile1); oneOf(mockWorkspaceFileValidator).triggerSchematronValidationForFile(workspaceID, mockFile1); oneOf(mockTypecheckedResults).getCheckedMimetype(); will(returnValue(fileMimetype)); oneOf(mockNodeUtil).convertMimetype(fileMimetype); will(returnValue(fileNodeType)); oneOf(mockMetadataApiBridge).getSelfHandleFromDocument(mockMetadataDocument); will(returnValue(uploadedFileArchiveURI)); oneOf(mockHandleParser).prepareAndValidateHandleWithHdlPrefix(uploadedFileArchiveURI); will(returnValue(completeFileArchiveURI)); oneOf(mockArchiveFileLocationProvider).isFileInOrphansDirectory(mockFile1); will(returnValue(Boolean.FALSE)); oneOf(mockMetadataDocument).getDocumentType(); will(returnValue(mockMetadataDocumentType)); oneOf(mockMetadataDocumentType).getSchemaLocation(); will(returnValue(schemaLocation)); oneOf(mockMetadataApiBridge).getDocumentNameForProfile(mockMetadataDocument, schemaLocation); will(returnValue(documentName)); oneOf(mockWorkspaceNodeFactory).getNewWorkspaceNodeFromFile(workspaceID, completeFileArchiveURI, null, uploadedFileURL, schemaLocation, documentName, fileMimetype, fileNodeType, WorkspaceNodeStatus.UPLOADED, Boolean.FALSE); will(returnValue(uploadedNode)); oneOf(mockWorkspaceDao).addWorkspaceNode(uploadedNode); //check links oneOf(mockWorkspaceUploadHelper).assureLinksInWorkspace(mockWorkspace, uploadedNodes); will(returnValue(failedLinks)); } }); Collection<ImportProblem> result = uploader.processUploadedFiles(workspaceID, uploadedFiles); assertNotNull("Collection with failed uploads should not be null", result); assertTrue("Collection with failed uploads should be empty", result.isEmpty()); }
From source file:nl.mpi.lamus.workspace.upload.implementation.LamusWorkspaceUploaderTest.java
@Test public void processOneUploadedMetadataFile_NoValidationIssues_NoMappedName() throws IOException, WorkspaceNodeNotFoundException, URISyntaxException, WorkspaceException, NodeNotFoundException, TypeCheckerException, Exception { final String filename = "someFile.cmdi"; final String documentName = "SomeFile"; final URI workspaceTopNodeArchiveURI = URI .create(handleProxyPlusPrefixWithSlash + UUID.randomUUID().toString()); final File workspaceTopNodeArchiveFile = new File("/archive/some/node.cmdi"); final File uploadedFile = new File(workspaceUploadDirectory, filename); final URI uploadedFileURI = uploadedFile.toURI(); final String uploadedFileRawHandle = UUID.randomUUID().toString(); final URI uploadedFileArchiveURI = URI.create(handlePrefixWithSlash + uploadedFileRawHandle); final URI completeFileArchiveURI = URI.create(handleProxyPlusPrefixWithSlash + uploadedFileRawHandle); final URL uploadedFileURL = uploadedFileURI.toURL(); final URI schemaLocation = URI.create("http://some/location/schema.xsd"); final WorkspaceNodeType fileNodeType = WorkspaceNodeType.METADATA; final String fileMimetype = "text/x-cmdi-xml"; final WorkspaceNode uploadedNode = new LamusWorkspaceNode(workspaceID, null, null); uploadedNode.setName(documentName);//from ww w . java 2 s. c om uploadedNode.setStatus(WorkspaceNodeStatus.UPLOADED); uploadedNode.setType(fileNodeType); uploadedNode.setFormat(fileMimetype); uploadedNode.setWorkspaceURL(uploadedFileURL); uploadedNode.setArchiveURI(uploadedFileArchiveURI); final Collection<File> uploadedFiles = new ArrayList<>(); uploadedFiles.add(mockFile1); final Collection<WorkspaceNode> uploadedNodes = new ArrayList<>(); uploadedNodes.add(uploadedNode); //only one file in the collection, so only one loop cycle final Collection<ImportProblem> failedLinks = new ArrayList<>(); context.checking(new Expectations() { { oneOf(mockWorkspaceDao).getWorkspace(workspaceID); will(returnValue(mockWorkspace)); oneOf(mockWorkspaceDao).getWorkspaceTopNode(workspaceID); will(returnValue(mockWorkspaceTopNode)); oneOf(mockWorkspaceTopNode).getArchiveURI(); will(returnValue(workspaceTopNodeArchiveURI)); oneOf(mockNodeDataRetriever).getNodeLocalFile(workspaceTopNodeArchiveURI); will(returnValue(workspaceTopNodeArchiveFile)); //loop allowing(mockFile1).getName(); will(returnValue(filename)); oneOf(mockFile1).toURI(); will(returnValue(uploadedFileURI)); oneOf(mockNodeDataRetriever).triggerResourceFileCheck(uploadedFileURL, filename); will(returnValue(mockTypecheckedResults)); oneOf(mockNodeDataRetriever).isCheckedResourceArchivable(with(same(mockTypecheckedResults)), with(same(workspaceTopNodeArchiveFile)), with(any(StringBuilder.class))); will(returnValue(Boolean.TRUE)); oneOf(mockMetadataAPI).getMetadataDocument(uploadedFileURL); will(returnValue(mockMetadataDocument)); oneOf(mockWorkspaceFileValidator).triggerSchemaValidationForFile(workspaceID, mockFile1); oneOf(mockWorkspaceFileValidator).triggerSchematronValidationForFile(workspaceID, mockFile1); oneOf(mockTypecheckedResults).getCheckedMimetype(); will(returnValue(fileMimetype)); oneOf(mockNodeUtil).convertMimetype(fileMimetype); will(returnValue(fileNodeType)); oneOf(mockMetadataApiBridge).getSelfHandleFromDocument(mockMetadataDocument); will(returnValue(uploadedFileArchiveURI)); oneOf(mockHandleParser).prepareAndValidateHandleWithHdlPrefix(uploadedFileArchiveURI); will(returnValue(completeFileArchiveURI)); oneOf(mockArchiveFileLocationProvider).isFileInOrphansDirectory(mockFile1); will(returnValue(Boolean.FALSE)); oneOf(mockMetadataDocument).getDocumentType(); will(returnValue(mockMetadataDocumentType)); oneOf(mockMetadataDocumentType).getSchemaLocation(); will(returnValue(schemaLocation)); oneOf(mockMetadataApiBridge).getDocumentNameForProfile(mockMetadataDocument, schemaLocation); will(returnValue(null)); oneOf(mockMetadataDocument).getDisplayValue(); will(returnValue(documentName)); oneOf(mockWorkspaceNodeFactory).getNewWorkspaceNodeFromFile(workspaceID, completeFileArchiveURI, null, uploadedFileURL, schemaLocation, documentName, fileMimetype, fileNodeType, WorkspaceNodeStatus.UPLOADED, Boolean.FALSE); will(returnValue(uploadedNode)); oneOf(mockWorkspaceDao).addWorkspaceNode(uploadedNode); //check links oneOf(mockWorkspaceUploadHelper).assureLinksInWorkspace(mockWorkspace, uploadedNodes); will(returnValue(failedLinks)); } }); Collection<ImportProblem> result = uploader.processUploadedFiles(workspaceID, uploadedFiles); assertNotNull("Collection with failed uploads should not be null", result); assertTrue("Collection with failed uploads should be empty", result.isEmpty()); }
From source file:nl.mpi.lamus.workspace.upload.implementation.LamusWorkspaceUploaderTest.java
@Test public void processOneUploadedMetadataFile_withOneValidationIssue_Warning() throws URISyntaxException, MalformedURLException, WorkspaceNodeNotFoundException, NodeNotFoundException, TypeCheckerException, MetadataValidationException, WorkspaceException, IOException, MetadataException, CMDIValidatorInitException {// www . j a va 2 s. co m final String filename = "someFile.cmdi"; final String documentName = "SomeFile"; final URI workspaceTopNodeArchiveURI = URI .create(handleProxyPlusPrefixWithSlash + UUID.randomUUID().toString()); final File workspaceTopNodeArchiveFile = new File("/archive/some/node.cmdi"); final File uploadedFile = new File(workspaceUploadDirectory, filename); final URI uploadedFileURI = uploadedFile.toURI(); final String uploadedFileRawHandle = UUID.randomUUID().toString(); final URI uploadedFileArchiveURI = URI.create(handlePrefixWithSlash + uploadedFileRawHandle); final URI completeFileArchiveURI = URI.create(handleProxyPlusPrefixWithSlash + uploadedFileRawHandle); final URL uploadedFileURL = uploadedFileURI.toURL(); final URI schemaLocation = URI.create("http://some/location/schema.xsd"); final WorkspaceNodeType fileNodeType = WorkspaceNodeType.METADATA; final String fileMimetype = "text/x-cmdi-xml"; final WorkspaceNode uploadedNode = new LamusWorkspaceNode(workspaceID, null, null); uploadedNode.setName(filename); uploadedNode.setStatus(WorkspaceNodeStatus.UPLOADED); uploadedNode.setType(fileNodeType); uploadedNode.setFormat(fileMimetype); uploadedNode.setWorkspaceURL(uploadedFileURL); uploadedNode.setArchiveURI(uploadedFileArchiveURI); final Collection<File> uploadedFiles = new ArrayList<>(); uploadedFiles.add(mockFile1); final Collection<MetadataValidationIssue> issues = new ArrayList<>(); issues.add(mockValidationIssue1); final String assertionErrorMessage = "[CMDI Best Practice] /cmd:CMD/cmd:Components/*/cmd:Title shouldn't be empty."; final String validationIssuesString = "Validation issue for file '" + filename + "' - " + MetadataValidationIssueSeverity.WARN.toString() + ": " + assertionErrorMessage + ".\n"; final MetadataValidationException expectedException = new MetadataValidationException( validationIssuesString, workspaceID, null); expectedException.addValidationIssues(issues); final Collection<WorkspaceNode> uploadedNodes = new ArrayList<>(); uploadedNodes.add(uploadedNode); //only one file in the collection, so only one loop cycle context.checking(new Expectations() { { oneOf(mockWorkspaceDao).getWorkspace(workspaceID); will(returnValue(mockWorkspace)); oneOf(mockWorkspaceDao).getWorkspaceTopNode(workspaceID); will(returnValue(mockWorkspaceTopNode)); oneOf(mockWorkspaceTopNode).getArchiveURI(); will(returnValue(workspaceTopNodeArchiveURI)); oneOf(mockNodeDataRetriever).getNodeLocalFile(workspaceTopNodeArchiveURI); will(returnValue(workspaceTopNodeArchiveFile)); //loop allowing(mockFile1).getName(); will(returnValue(filename)); oneOf(mockFile1).toURI(); will(returnValue(uploadedFileURI)); oneOf(mockNodeDataRetriever).triggerResourceFileCheck(uploadedFileURL, filename); will(returnValue(mockTypecheckedResults)); oneOf(mockNodeDataRetriever).isCheckedResourceArchivable(with(same(mockTypecheckedResults)), with(same(workspaceTopNodeArchiveFile)), with(any(StringBuilder.class))); will(returnValue(Boolean.TRUE)); oneOf(mockMetadataAPI).getMetadataDocument(uploadedFileURL); will(returnValue(mockMetadataDocument)); oneOf(mockWorkspaceFileValidator).triggerSchemaValidationForFile(workspaceID, mockFile1); oneOf(mockWorkspaceFileValidator).triggerSchematronValidationForFile(workspaceID, mockFile1); will(throwException(expectedException)); oneOf(mockWorkspaceFileValidator) .validationIssuesToString(with(equivalentValidationIssueCollection(issues))); will(returnValue(validationIssuesString)); oneOf(mockWorkspaceFileValidator) .validationIssuesContainErrors(with(equivalentValidationIssueCollection(issues))); will(returnValue(Boolean.FALSE)); oneOf(mockTypecheckedResults).getCheckedMimetype(); will(returnValue(fileMimetype)); oneOf(mockNodeUtil).convertMimetype(fileMimetype); will(returnValue(fileNodeType)); oneOf(mockMetadataApiBridge).getSelfHandleFromDocument(mockMetadataDocument); will(returnValue(uploadedFileArchiveURI)); oneOf(mockHandleParser).prepareAndValidateHandleWithHdlPrefix(uploadedFileArchiveURI); will(returnValue(completeFileArchiveURI)); oneOf(mockArchiveFileLocationProvider).isFileInOrphansDirectory(mockFile1); will(returnValue(Boolean.FALSE)); oneOf(mockMetadataDocument).getDocumentType(); will(returnValue(mockMetadataDocumentType)); oneOf(mockMetadataDocumentType).getSchemaLocation(); will(returnValue(schemaLocation)); oneOf(mockMetadataApiBridge).getDocumentNameForProfile(mockMetadataDocument, schemaLocation); will(returnValue(documentName)); oneOf(mockWorkspaceNodeFactory).getNewWorkspaceNodeFromFile(workspaceID, completeFileArchiveURI, null, uploadedFileURL, schemaLocation, documentName, fileMimetype, fileNodeType, WorkspaceNodeStatus.UPLOADED, Boolean.FALSE); will(returnValue(uploadedNode)); oneOf(mockWorkspaceDao).addWorkspaceNode(uploadedNode); //still calls method to process links oneOf(mockWorkspaceUploadHelper).assureLinksInWorkspace(mockWorkspace, uploadedNodes); } }); Collection<ImportProblem> result = uploader.processUploadedFiles(workspaceID, uploadedFiles); assertNotNull("Collection with failed uploads should not be null", result); assertTrue("Collection with failed uploads should be empty", result.isEmpty()); }
From source file:org.cytoscape.io.internal.read.GenericReaderManager.java
/** * Gets the GraphReader that is capable of reading the specified file. * /*from www . jav a 2s .c o m*/ * @param uri * URI of file to be read. * @return GraphReader capable of reading the specified file. Null if file * cannot be read. */ public R getReader(final URI uri, final String inputName) { // Data location is always required. if (uri == null) { throw new NullPointerException("Data source URI is null"); } // This is the default reader, which accepts files with no extension. // Usually, this is ImportNetworkTableReaderFactory (Manual table // import) T defaultFactory = null; final List<T> factoryList = new ArrayList<T>(); final Map<String, T> factoryTable = new HashMap<String, T>(); // Pick compatible reader factories. for (final T factory : factories) { final CyFileFilter cff = factory.getFileFilter(); // Got "Accepted" flag. Need to check it's default or not. if (cff.accepts(uri, category)) { logger.info("Filter returns Accepted. Need to check priority: " + factory); if (factory.getClass().toString().contains(DEFAULT_READER_FACTORY_CLASS)) { defaultFactory = factory; } else { factoryList.add(factory); for (final String extension : cff.getExtensions()) { factoryTable.put(extension, factory); } } } } T chosenFactory = null; // No compatible factory is available. if (factoryTable.isEmpty() && defaultFactory == null) { logger.warn("No reader found for uri: " + uri.toString()); throw new NullPointerException("Could not find reader."); } else if (factoryList.size() == 1) { // There is only one compatible reader factory. Use it: chosenFactory = factoryList.get(0); } else { if (factoryList.isEmpty() && defaultFactory != null) { // There is only one factory chosenFactory = defaultFactory; } else { // Well, we cannot decide which one is correct. Try to use ext... String extension = FilenameUtils.getExtension(uri.toString()); if (factoryTable.containsKey(extension)) chosenFactory = factoryTable.get(extension); else { if (factoryTable.containsKey("")) chosenFactory = factoryTable.get(""); else throw new NullPointerException("Could not find reader factory."); } } } try { logger.info("Successfully found compatible ReaderFactory " + chosenFactory); // This returns strean using proxy if it exists. InputStream stream = streamUtil.getInputStream(uri.toURL()); if (!stream.markSupported()) { stream = new BufferedInputStream(stream); } return (R) chosenFactory.createTaskIterator(stream, inputName).next(); } catch (IOException e) { logger.warn("Error opening stream to URI: " + uri.toString(), e); throw new IllegalStateException("Could not open stream for reader.", e); } }
From source file:org.kitodo.production.metadata.MetadataProcessor.java
/** * Convert the TIFF images of the current process to PNG images for the metadata web frontend and * copy them to them to the webapps/images/[processID]/fullsize/ folder. *//*from w ww. j a v a2 s .c o m*/ private void convertImages() { if (Objects.nonNull(this.currentTifFolder)) { try { ensureDirectoryExists(Paths.get(fullsizePath)); // first, convert tiff images to pngs for (URI tiffPath : this.imageHelper.getImageFiles(this.currentTifFolder)) { String targetPath = fullsizePath + FilenameUtils.removeExtension(tiffPath.toString()) + ".png"; File fullsizeFile = new File(targetPath); if (fullsizeFile.exists()) { continue; } URI tiffURI = Paths .get(ConfigCore.getKitodoDataDirectory() + this.currentTifFolder + tiffPath.toString()) .toUri(); logger.info("Reading {}", tiffURI); BufferedImage inputImage = ImageIO.read(tiffURI.toURL()); logger.info("Writing {}", targetPath); ImageIO.write(inputImage, "png", new File(targetPath)); numberOfConvertedImages++; // FIXME: this call to the update function does not work! updateComponent(metadataEditorComponents); } // then, create thumbnails from the converted images generateThumbnails(); updateComponent(metadataEditorComponents); } catch (MalformedURLException e) { Helper.setErrorMessage("ERROR: URL malformed!", logger, e); } catch (IOException e) { Helper.setErrorMessage("ERROR: IOException!", logger, e); } } }
From source file:net.www_eee.portal.channels.ProxyChannel.java
/** * <p>/* www .java2 s . c om*/ * Rewrite a <code>linkURI</code> associated with a <code>proxiedFileURL</code>, if required. * </p> * * <p> * Technically, there are two types of links within a document. First, a link within a document can be to an * <em>external resource</em>, which is loaded automatically by the browser to augment that document (ie a link to an * image, style sheet, script, etc). Or, second, a link within a document can be a <em>hyperlink</em>, which, when * activated by the user, will cause the browser to stop displaying that document and navigate to displaying the * linked document instead. * </p> * * <p> * If the portal is configured to display a website to clients through this <code>ProxyChannel</code>, it is generally * expected that if the client navigates a hyperlink from one document to another within the proxied site, that the * linked document would also be rendered within the channel ({@linkplain net.www_eee.portal.Channel.Mode#VIEW view * mode}), and that any external resource links will continue to resolve correctly. Link rewriting is required for * each of these two scenarios to work. * </p> * * <p> * To continue rendering within {@linkplain net.www_eee.portal.Channel.Mode#VIEW view mode} while navigating between * website documents, any hyperlink from within a proxied document to another document within the * {@linkplain #getProxiedBaseURI(Page.Request) proxied site} will, by default, be rewritten to point back through * this channel (alternatively, hyperlinks may {@linkplain #isLinkRewritingHyperlinksToChannelDisabled(Page.Request) * optionally} be resolved into an {@linkplain URI#isAbsolute() absolute} link pointing directly back to their * {@linkplain #getProxiedBaseURI(Page.Request) origin/source location} instead). * </p> * * <p> * If this channel were to blindly return unmodified source HTML from a proxied document for aggregation into a * {@link Page}, any relative link would break when it was incorrectly resolved relative to the * {@link net.www_eee.portal.ContentDef.Page.Key#getPageURI(UriInfo, Map, String, boolean) URL} of that page, instead * of relative to the {@linkplain #BASE_URI_PROP base URL} of the document providing it. To avoid this, any relative * link to an external resource from within a proxied document will, by default, be resolved into an * {@linkplain URI#isAbsolute() absolute} link pointing directly back to the * {@linkplain #getProxiedBaseURI(Page.Request) origin/source location} for that resource (alternatively, resource * links may {@linkplain #isLinkRewritingResourceLinksToChannelEnabled(Page.Request) optionally} be rewritten to point * back through this channel using {@linkplain net.www_eee.portal.Channel.Mode#RESOURCE resource mode} instead). * </p> * * <p> * For link rewriting to work, the <code>ProxyChannel</code> obviously needs to know which attributes of a proxied * document constitute <em>links</em>. But since the implementation is generic, and doesn't actually understand any * particular dialect of markup language on it's own, <em>including HTML</em>, you will likely want to configure this * channel alongside a plugin which does, such as the * {@linkplain net.www_eee.portal.channelplugins.ProxyChannelHTMLSource HTML plugin}. * </p> * * @param pageRequest The {@link net.www_eee.portal.Page.Request Request} currently being processed. * @param proxiedFileURL The {@linkplain #getProxiedFileURL(Page.Request, Channel.Mode, boolean) proxied file URL}. * @param linkURI The {@link URI} of the link to rewrite. * @param hyperlink Is the <code>linkURI</code> a hyperlink? * @param absoluteURLRequired Does the result need to be {@linkplain URI#isAbsolute() absolute}? * @return A Map.Entry containing the rewritten link value, and a Boolean specifying if the returned link points back * through the channel. * @throws WWWEEEPortal.Exception If a problem occurred while determining the result. * @see #isLinkRewritingHyperlinksToChannelDisabled(Page.Request) * @see #isLinkRewritingResourceLinksToChannelEnabled(Page.Request) * @see net.www_eee.portal.channelplugins.ProxyChannelHTMLSource */ public Map.Entry<URI, Boolean> rewriteProxiedFileLink(final Page.Request pageRequest, final URL proxiedFileURL, final @Nullable URI linkURI, final boolean hyperlink, final boolean absoluteURLRequired) throws WWWEEEPortal.Exception { if ((linkURI != null) && (linkURI.isOpaque())) { return new AbstractMap.SimpleImmutableEntry<URI, Boolean>(linkURI, Boolean.FALSE); // Leave all the opaque ones alone (stuff like "mailto" links, etc), as we can't do anything with those. } final @NonNull URL resolvedLinkURL; // First, resolve the URL for the link so we know what server+file we are actually talking about here. try { if (linkURI == null) { resolvedLinkURL = proxiedFileURL; // An empty (null) link is equivalent to one back to the same proxied file. } else if (linkURI.isAbsolute()) { resolvedLinkURL = linkURI.toURL(); } else { resolvedLinkURL = new URL(proxiedFileURL, linkURI.toString()); // Resolve the link relative to the file it came from. } } catch (MalformedURLException mue) { throw new ContentManager.ContentException("Error resolving proxied link URL", mue); } if (((hyperlink) && (isLinkRewritingHyperlinksToChannelDisabled(pageRequest))) || ((!hyperlink) && (!isLinkRewritingResourceLinksToChannelEnabled(pageRequest)))) { // We are configured to not write this link back through the portal. return new AbstractMap.SimpleImmutableEntry<URI, Boolean>( rewriteProxiedFileLinkOutsideChannel(pageRequest, proxiedFileURL, linkURI, hyperlink, absoluteURLRequired, resolvedLinkURL), Boolean.FALSE); } /* * At this point, in order to determine what modifications to the link might be required, we need to figure out if * the link points to something either within, or outside of, the channel base URI's folder? */ if ((linkURI != null) && (linkURI.isAbsolute()) && (!equalHostAndPort(resolvedLinkURL, proxiedFileURL))) { // This is an absolute link which doesn't even point to the same server as the proxied file. return new AbstractMap.SimpleImmutableEntry<URI, Boolean>( rewriteProxiedFileLinkOutsideChannel(pageRequest, proxiedFileURL, linkURI, hyperlink, absoluteURLRequired, resolvedLinkURL), Boolean.FALSE); } /* * At this point we know the link at least points to the same server as the proxied file, but is it within the * channel base URI's folder? */ final String resolvedLinkPath = StringUtil.toString(StringUtil.mkNull(resolvedLinkURL.getPath()), "/"); final URI baseURI = getProxiedBaseURI(pageRequest); final URI resolvedBaseURI; if (baseURI.isAbsolute()) { resolvedBaseURI = baseURI; } else { resolvedBaseURI = ConfigManager.getContextResourceLocalHostURI(pageRequest.getUriInfo(), baseURI.getPath(), NetUtil.getQueryParams(baseURI), baseURI.getFragment(), true); } final String baseURIPath = resolvedBaseURI.getPath(); final String baseURIFolder; if ((baseURIPath.length() == 1) || (baseURIPath.charAt(baseURIPath.length() - 1) == '/')) { baseURIFolder = baseURIPath; // Path is a folder. } else { final int lastSlashIndex = baseURIPath.lastIndexOf('/'); baseURIFolder = (lastSlashIndex > 0) ? baseURIPath.substring(0, lastSlashIndex + 1) : String.valueOf('/'); } if (!resolvedLinkPath.startsWith(baseURIFolder)) { // We have determined this link is not within the channel base URI folder. return new AbstractMap.SimpleImmutableEntry<URI, Boolean>( rewriteProxiedFileLinkOutsideChannel(pageRequest, proxiedFileURL, linkURI, hyperlink, absoluteURLRequired, resolvedLinkURL), Boolean.FALSE); } /* * At this point we know the link points to within the channel base URI's folder, and that we need to rewrite it to * point back through the channel. */ final String linkChannelLocalPath = StringUtil.mkNull(resolvedLinkPath.substring(baseURIFolder.length())); final Mode channelMode = ((hyperlink) && (!isMaximizationDisabled(pageRequest))) ? Mode.VIEW : Mode.RESOURCE; final ContentDef.ChannelSpec<?> channelSpec = pageRequest.getChannelSpec(this); return new AbstractMap.SimpleImmutableEntry<URI, Boolean>( channelSpec.getKey().getChannelURI(pageRequest.getUriInfo(), channelMode, linkChannelLocalPath, (linkURI != null) ? NetUtil.getQueryParams(linkURI) : null, (linkURI != null) ? linkURI.getFragment() : null, absoluteURLRequired), Boolean.TRUE); }