@Test
  public void identifyZipFileTest() throws CommandExecutionException {

    zipIdentifierEngine.setRequestFactory(requestFactory);
    zipContainerContentIdentifier.setIdentifierEngine(zipIdentifierEngine);
    Object object = zipContainerContentIdentifier.getIdentifierEngine();
    assertEquals(object.getClass(), zipIdentifierEngine.getClass());

    try {
      InputStream in = new FileInputStream(containerSignatures);
      ContainerSignatureSaxParser parser = new ContainerSignatureSaxParser();
      containerSignatureDefinitions = parser.parse(in);
    } catch (SignatureParseException e) {
      throw new CommandExecutionException("Can't parse container signature file");
    } catch (Exception e) {
      throw new CommandExecutionException(e);
    }

    zipContainerContentIdentifier.init(containerSignatureDefinitions, "ZIP");
    object = zipContainerContentIdentifier.getContainerIdentifierInit();
    assertEquals(object.getClass(), containerIdentifierInit.getClass());

    object = zipContainerContentIdentifier.getFormats();
    assertEquals(object.getClass(), formats.getClass());

    String fileName;
    File file = new File(zipFile);
    if (!file.exists()) {
      fail("ZIP test file not found");
    }
    URI uri = file.toURI();
    RequestIdentifier identifier = new RequestIdentifier(uri);
    identifier.setParentId(1L);
    try {
      fileName = file.getCanonicalPath();
      RequestMetaData metaData = new RequestMetaData(file.length(), file.lastModified(), fileName);
      FileSystemIdentificationRequest request =
          new FileSystemIdentificationRequest(metaData, identifier);
      IdentificationResultCollection results = new IdentificationResultCollection(request);
      request.open(file);
      results = zipContainerContentIdentifier.process(request.getSourceInputStream(), results);
      if (results.getResults().isEmpty()) {
        fail("ZIP file not identified");
      }
      String fmtExpected = "";
      for (IdentificationResult identResult : results.getResults()) {
        if (identResult.getPuid().equals("fmt/189")) {
          fmtExpected = identResult.getPuid();
        }
      }
      assertEquals(fmtExpected, "fmt/189");
    } catch (IOException e) {
      throw new CommandExecutionException(e);
    }
  }
  private String getContainerFormat(IdentificationResultCollection results) {
    final List<IdentificationResult> theResults = results.getResults();
    final int numResults =
        theResults.size(); // use an indexed loop to reduce garbage, don't allocate an iterator.
    for (int i = 0; i < numResults; i++) {
      final IdentificationResult result = theResults.get(i);
      final String format = containerFormatResolver.forPuid(result.getPuid());
      if (format != null) {
        return format;
      }
    }

    return null;
  }
  /**
   * @param results A previous identification of an archival format.
   * @return format or null
   */
  private String getArchiveFormat(IdentificationResultCollection results) {
    final List<IdentificationResult> theResults = results.getResults();
    final int numResults =
        theResults.size(); // use an indexed loop to reduce garbage, don't allocate an iterator.
    for (int i = 0; i < numResults; i++) {
      final IdentificationResult result = theResults.get(i);
      String format = archiveFormatResolver.forPuid(result.getPuid());
      if (format != null) { // exit on the first non-null format met
        if (processArchives && !processWebArchives && isWebArchiveFormat(format)) {
          format = null;
        } else if (!processArchives && processWebArchives && !isWebArchiveFormat(format)) {
          format = null;
        }
        return format;
      }
    }

    return null;
  }
  /*
   ** Similar to above - except that there is no second occurrence of the final left fragment - and
   ** the sole occurrence of this fragment is beyond the maximum offset for the sequence as a whole
   ** from BOF. N.B. This test passes in the previous version DROID 6.2.1
   */
  @Test
  public void testFinalLeftFragmentBeyondSeqMaxOffset() throws Exception {

    final String sigFile = "left-frag-1200.xml";
    final String fileToScan = "left1200a.ext";
    final String expectedPuid = "dev/1200";

    BinarySignatureIdentifier droid = new BinarySignatureIdentifier();
    droid.setSignatureFile(TESTAREA + sigFile);
    try {
      droid.init();
    } catch (SignatureParseException x) {
      assertEquals("Can't parse signature file", x.getMessage());
    }
    File file = new File(TESTAREA + fileToScan);
    assertTrue(file.exists());
    URI resourceUri = file.toURI();

    RequestMetaData metaData = new RequestMetaData(file.length(), file.lastModified(), fileToScan);
    RequestIdentifier identifier = new RequestIdentifier(resourceUri);
    identifier.setParentId(1L);

    IdentificationRequest<File> request = new FileSystemIdentificationRequest(metaData, identifier);
    request.open(file);

    IdentificationResultCollection resultsCollection = droid.matchBinarySignatures(request);
    List<IdentificationResult> results = resultsCollection.getResults();

    assertEquals(0, results.size());

    Iterator<IdentificationResult> iter = results.iterator();
    while (iter.hasNext()) {
      IdentificationResult result = iter.next();
      assertEquals(expectedPuid, result.getPuid());
    }
  }
Пример #5
0
    /**
     * Run droid identification on file
     *
     * @param filePath Absolute file path
     * @return Result list
     * @throws FileNotFoundException
     * @throws IOException
     */
    @Override
    public HashMap<String, String> identify(File file) throws FileNotFoundException {
        HashMap<String, String> droidIdRes = new HashMap<String, String>();
        InputStream in = null;
        IdentificationRequest request = null;

        try {
            URI resourceUri = file.toURI();
            in = new FileInputStream(file);
            LOG.debug("Identification of resource: " + resourceUri.toString());
            RequestMetaData metaData = new RequestMetaData(file.length(), file.lastModified(), file.getName());
            LOG.debug("File length: " + file.length());
            LOG.debug("File modified: " + file.lastModified());
            LOG.debug("File name: " + file.getName());
            RequestIdentifier identifier = new RequestIdentifier(resourceUri);
            request = new FileSystemIdentificationRequest(metaData, identifier);
            request.open(in);
            IdentificationResultCollection results = bsi.matchBinarySignatures(request);
            bsi.removeLowerPriorityHits(results);
            if (results == null || results.getResults() == null || results.getResults().isEmpty()) {
                LOG.debug("No identification result");
            } else {
                List<IdentificationResult> result = results.getResults();
                if (result != null && !result.isEmpty()) {
                    for (IdentificationResult ir : result) {
                        String mime = ir.getMimeType();
                        if (mime != null && !mime.isEmpty()) {
                            // take first mime, ignore others
                            if (!droidIdRes.containsKey("mime")) {
                                droidIdRes.put("mime", mime);
                            }
                        }
                        String puid = ir.getPuid();
                        if (puid != null && !puid.isEmpty()) {
                            // take first puid, ignore others
                            if (!droidIdRes.containsKey("puid")) {
                                droidIdRes.put("puid", puid);
                            }
                        }
                    }
                }
            }
            in.close();
            request.close();
        } catch (IOException ex) {
            LOG.error("I/O Exception", ex);
        } finally {
            try {
                in.close();
                request.close();
            } catch (IOException _) {
            }
        }
        if (!droidIdRes.containsKey("mime")) {
            droidIdRes.put("mime", MIME_UNKNOWN);
        }
        if (!droidIdRes.containsKey("puid")) {
            droidIdRes.put("puid", "fmt/0");
        }
        return droidIdRes;
    }