org.apache.tika.mime.MimeTypeException Java Examples

The following examples show how to use org.apache.tika.mime.MimeTypeException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TikaFilePlace.java    From emissary with Apache License 2.0 6 votes vote down vote up
/**
 * Set up place specific information from the config file, load JNI, etc.
 */
protected void configurePlace() throws IOException {

    configureIdPlace(); // pick up ID_IGNORE types
    tikaSignaturePaths = configG.findEntries("TIKA_SIGNATURE_FILE", DEFAULT_TIKA_SIGNATURE_FILE);
    includeFilenameMimeType = configG.findBooleanEntry("INCLUDE_FILENAME_MIME_TYPE", includeFilenameMimeType);

    try {
        InputStream[] tikaSignatures = getTikaSignatures();
        mimeTypes = MimeTypesFactory.create(tikaSignatures);
    } catch (MimeTypeException e) {
        logger.error("Error loading tika configuration: " + tikaSignaturePaths.toString(), e);
        throw new IOException("Error loading tika configuration" + tikaSignaturePaths.toString());
    }

    for (Map.Entry<String, String> entry : configG.findStringMatchMap("MIN_SIZE_").entrySet()) {
        try {
            minSizeMap.put(entry.getKey(), Integer.parseInt(entry.getValue()));
        } catch (NumberFormatException ex) {
            logger.info("Must be numeric MIN_SIZE_" + entry.getKey() + " = " + entry.getValue());
        }
    }
}
 
Example #2
Source File: TikaUtil.java    From scipio-erp with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the top tika mime-type definition for the media type.
 * WARN: this only returns explicit defined mime-types (canonical), NOT aliases.
 * if exact true, will double-check that parameters match as well (not guaranteed by MimeTypes.getRegisteredMimeType).
 * FIXME: exact doesn't handle parameter order.
 */
public static MimeType getMimeTypeForMediaTypeSafe(String mediaType, MimeTypes mimeTypes, boolean exact) {
    try {
        MimeType mimeType = mimeTypes.getRegisteredMimeType(mediaType);
        if (mimeType != null && exact) {
            // NOTE: because the way getRegisteredMimeType works, it may return non-null
            // even if not exact name match, due to parameters.
            // FIXME: this check won't handle parameter order difference
            // also check if another normalize call would be more appropriate...
            if (!getMimeTypeId(mediaType).equals(getMimeTypeId(mimeType.getName()))) {
                return null;
            }
        }
        return mimeType;
    } catch (MimeTypeException e) {
        return null;
    }
}
 
Example #3
Source File: MediaUtils.java    From desktopclient-java with GNU General Public License v3.0 6 votes vote down vote up
public static String extensionForMIME(String mimeType) {
    if (mimeType.isEmpty())
        return DEFAULT_EXT;

    MimeType mime = null;
    try {
        mime = MimeTypes.getDefaultMimeTypes().forName(mimeType);
    } catch (MimeTypeException ex) {
        LOGGER.log(Level.WARNING, "can't find mimetype", ex);
    }

    String m = mime != null ? mime.getExtension() : "";
    // remove dot
    if (!m.isEmpty())
        m = m.substring(1);
    return StringUtils.defaultIfEmpty(m, DEFAULT_EXT);
}
 
Example #4
Source File: MimeUtil.java    From anthelion with Apache License 2.0 5 votes vote down vote up
/**
 * A facade interface to Tika's underlying {@link MimeTypes#forName(String)}
 * method.
 *
 * @param name
 *          The name of a valid {@link MimeType} in the Tika mime registry.
 * @return The object representation of the {@link MimeType}, if it exists,
 *         or null otherwise.
 */
public String forName(String name) {
  try {
    return this.mimeTypes.forName(name).toString();
  } catch (MimeTypeException e) {
    LOG.error("Exception getting mime type by name: [" + name
        + "]: Message: " + e.getMessage());
    return null;
  }
}
 
Example #5
Source File: MimeUtil.java    From nutch-htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * A facade interface to Tika's underlying {@link MimeTypes#forName(String)}
 * method.
 *
 * @param name
 *          The name of a valid {@link MimeType} in the Tika mime registry.
 * @return The object representation of the {@link MimeType}, if it exists,
 *         or null otherwise.
 */
public String forName(String name) {
  try {
    return this.mimeTypes.forName(name).toString();
  } catch (MimeTypeException e) {
    LOG.error("Exception getting mime type by name: [" + name
        + "]: Message: " + e.getMessage());
    return null;
  }
}
 
Example #6
Source File: Reference.java    From oodt with Apache License 2.0 5 votes vote down vote up
/**
 * <p>
 * Constructs a new Reference with the specified parameters.
 * </p>
 * 
 * @param origRef
 *            The item's original location.
 * @param dataRef
 *            The item's location within the data store.
 * @param size
 *            The size of the file that this reference refers to.
 */
public Reference(String origRef, String dataRef, long size) {
    origReference = origRef;
    dataStoreReference = dataRef;
    fileSize = size;
    // TODO: since no mimetype was specified, do the dirty work
    // ourselves to determine the which MimeType class to associate
    // with this reference.
    try {
        this.mimeType = mimeTypeRepository.forName(new Tika().detect(origRef));
    } catch (MimeTypeException e) {
        LOG.log(Level.SEVERE, e.getMessage());
    }

}
 
Example #7
Source File: Reference.java    From oodt with Apache License 2.0 5 votes vote down vote up
/**
 * @param name
 *            the String name of the mimetype of this reference
 */
public void setMimeType(String name) {
    if(name == null || (name.equals(""))) {
        return;
    }
    
    try {
      this.mimeType = mimeTypeRepository.forName(name);
    } catch (MimeTypeException e) {
       LOG.log(Level.SEVERE, e.getMessage());
    }
}
 
Example #8
Source File: IdentifyMimeType.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }

    final ComponentLog logger = getLogger();
    final AtomicReference<String> mimeTypeRef = new AtomicReference<>(null);
    final String filename = flowFile.getAttribute(CoreAttributes.FILENAME.key());

    session.read(flowFile, new InputStreamCallback() {
        @Override
        public void process(final InputStream stream) throws IOException {
            try (final InputStream in = new BufferedInputStream(stream)) {
                TikaInputStream tikaStream = TikaInputStream.get(in);
                Metadata metadata = new Metadata();
                // Add filename if it exists
                if (filename != null) {
                    metadata.add(TikaMetadataKeys.RESOURCE_NAME_KEY, filename);
                }
                // Get mime type
                MediaType mediatype = detector.detect(tikaStream, metadata);
                mimeTypeRef.set(mediatype.toString());
            }
        }
    });

    String mimeType = mimeTypeRef.get();
    String extension = "";
    try {
        MimeType mimetype;
        mimetype = config.getMimeRepository().forName(mimeType);
        extension = mimetype.getExtension();
    } catch (MimeTypeException ex) {
        logger.warn("MIME type extension lookup failed: {}", new Object[]{ex});
    }

    // Workaround for bug in Tika - https://issues.apache.org/jira/browse/TIKA-1563
    if (mimeType != null && mimeType.equals("application/gzip") && extension.equals(".tgz")) {
        extension = ".gz";
    }

    if (mimeType == null) {
        flowFile = session.putAttribute(flowFile, CoreAttributes.MIME_TYPE.key(), "application/octet-stream");
        flowFile = session.putAttribute(flowFile, "mime.extension", "");
        logger.info("Unable to identify MIME Type for {}; setting to application/octet-stream", new Object[]{flowFile});
    } else {
        flowFile = session.putAttribute(flowFile, CoreAttributes.MIME_TYPE.key(), mimeType);
        flowFile = session.putAttribute(flowFile, "mime.extension", extension);
        logger.info("Identified {} as having MIME Type {}", new Object[]{flowFile, mimeType});
    }

    session.getProvenanceReporter().modifyAttributes(flowFile);
    session.transfer(flowFile, REL_SUCCESS);
}
 
Example #9
Source File: MimeUtil.java    From anthelion with Apache License 2.0 4 votes vote down vote up
/**
 * A facade interface to trying all the possible mime type resolution
 * strategies available within Tika. First, the mime type provided in
 * <code>typeName</code> is cleaned, with {@link #cleanMimeType(String)}.
 * Then the cleaned mime type is looked up in the underlying Tika
 * {@link MimeTypes} registry, by its cleaned name. If the {@link MimeType} is
 * found, then that mime type is used, otherwise URL resolution is
 * used to try and determine the mime type. If that means is unsuccessful, and
 * if <code>mime.type.magic</code> is enabled in {@link NutchConfiguration},
 * then mime type magic resolution is used to try and obtain a
 * better-than-the-default approximation of the {@link MimeType}.
 * 
 * @param typeName
 *          The original mime type, returned from a {@link ProtocolOutput}.
 * @param url
 *          The given @see url, that Nutch was trying to crawl.
 * @param data
 *          The byte data, returned from the crawl, if any.
 * @return The correctly, automatically guessed {@link MimeType} name.
 */
public String autoResolveContentType(String typeName, String url, byte[] data) {
  String retType = null;
  String magicType = null;
  MimeType type = null;
  String cleanedMimeType = null;

  try {
    cleanedMimeType = MimeUtil.cleanMimeType(typeName) != null ? this.mimeTypes
        .forName(MimeUtil.cleanMimeType(typeName)).getName()
        : null;
  } catch (MimeTypeException mte) {
    // Seems to be a malformed mime type name...
  }

  // first try to get the type from the cleaned type name
  try {
    type = cleanedMimeType != null ? this.mimeTypes.forName(cleanedMimeType)
        : null;
  } catch (MimeTypeException e) {
    type = null;
  }

  // if returned null, or if it's the default type then try url resolution
  if (type == null
      || (type != null && type.getName().equals(MimeTypes.OCTET_STREAM))) {
    // If no mime-type header, or cannot find a corresponding registered
    // mime-type, then guess a mime-type from the url pattern
    type = this.mimeTypes.getMimeType(url) != null ? this.mimeTypes
        .getMimeType(url) : type;
  }

  retType= type.getName();

  // if magic is enabled use mime magic to guess if the mime type returned
  // from the magic guess is different than the one that's already set so far
  // if it is, and it's not the default mime type, then go with the mime type
  // returned by the magic
  if (this.mimeMagic) {
    magicType = tika.detect(data);

    // Deprecated in Tika 1.0 See https://issues.apache.org/jira/browse/NUTCH-1230
    //MimeType magicType = this.mimeTypes.getMimeType(data);
    if (magicType != null && !magicType.equals(MimeTypes.OCTET_STREAM)
        && !magicType.equals(MimeTypes.PLAIN_TEXT)
        && retType != null && !retType.equals(magicType)) {

      // If magic enabled and the current mime type differs from that of the
      // one returned from the magic, take the magic mimeType
      retType = magicType;
    }

    // if type is STILL null after all the resolution strategies, go for the
    // default type
    if (retType == null) {
      try {
        retType = MimeTypes.OCTET_STREAM;
      } catch (Exception ignore) {
      }
    }
  }

  return retType;
}
 
Example #10
Source File: IdentifyMimeType.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }

    final ComponentLog logger = getLogger();
    final AtomicReference<String> mimeTypeRef = new AtomicReference<>(null);
    final String filename = flowFile.getAttribute(CoreAttributes.FILENAME.key());

    session.read(flowFile, new InputStreamCallback() {
        @Override
        public void process(final InputStream stream) throws IOException {
            try (final InputStream in = new BufferedInputStream(stream)) {
                TikaInputStream tikaStream = TikaInputStream.get(in);
                Metadata metadata = new Metadata();

                if (filename != null && context.getProperty(USE_FILENAME_IN_DETECTION).asBoolean()) {
                    metadata.add(TikaMetadataKeys.RESOURCE_NAME_KEY, filename);
                }
                // Get mime type
                MediaType mediatype = detector.detect(tikaStream, metadata);
                mimeTypeRef.set(mediatype.toString());
            }
        }
    });

    String mimeType = mimeTypeRef.get();
    String extension = "";
    try {
        MimeType mimetype;
        mimetype = mimeTypes.forName(mimeType);
        extension = mimetype.getExtension();
    } catch (MimeTypeException ex) {
        logger.warn("MIME type extension lookup failed: {}", new Object[]{ex});
    }

    // Workaround for bug in Tika - https://issues.apache.org/jira/browse/TIKA-1563
    if (mimeType != null && mimeType.equals("application/gzip") && extension.equals(".tgz")) {
        extension = ".gz";
    }

    if (mimeType == null) {
        flowFile = session.putAttribute(flowFile, CoreAttributes.MIME_TYPE.key(), "application/octet-stream");
        flowFile = session.putAttribute(flowFile, "mime.extension", "");
        logger.info("Unable to identify MIME Type for {}; setting to application/octet-stream", new Object[]{flowFile});
    } else {
        flowFile = session.putAttribute(flowFile, CoreAttributes.MIME_TYPE.key(), mimeType);
        flowFile = session.putAttribute(flowFile, "mime.extension", extension);
        logger.info("Identified {} as having MIME Type {}", new Object[]{flowFile, mimeType});
    }

    session.getProvenanceReporter().modifyAttributes(flowFile);
    session.transfer(flowFile, REL_SUCCESS);
}
 
Example #11
Source File: ReferenceResourceTest.java    From oodt with Apache License 2.0 4 votes vote down vote up
/**
 * Tests that {@link ReferenceResource reference resources} are marshalled to
 * the expected XML format.
 * @throws IOException if the {@link Diff} constructor fails
 * @throws JAXBException if the {@link JAXBContext} or {@link Marshaller} fail
 * @throws MimeTypeException if {@link MimeTypes#forName(String)} fails
 * @throws SAXException if the {@link Diff} constructor fails
 */
@Test
public void testXmlMarshalling() throws IOException, JAXBException,
  MimeTypeException, SAXException
{
  String productId = "123";
  int refIndex = 0;

  // Create a new ReferenceResource using a Reference instance.
  Reference reference = new Reference("original", "dataStore", 1000,
    new MimeTypes().forName("text/plain"));

  ReferenceResource resource = new ReferenceResource(productId, refIndex,
    reference, new File("/tmp"));


  // Generate the expected output.
  String expectedXml =
      "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
    + "<reference>"
    + "<productId>" + productId + "</productId>"
    + "<refIndex>" + refIndex + "</refIndex>"
    + "<dataStoreReference>"
    +    reference.getDataStoreReference()
    + "</dataStoreReference>"
    + "<originalReference>"
    +    reference.getOrigReference()
    + "</originalReference>"
    + "<mimeType>" + reference.getMimeType().getName() + "</mimeType>"
    + "<fileSize>" + reference.getFileSize() +  "</fileSize>"
    + "</reference>";


  // Set up a JAXB context and marshall the ReferenceResource to XML.
  JAXBContext context = JAXBContext.newInstance(resource.getClass());
  Marshaller marshaller = context.createMarshaller();
  StringWriter writer = new StringWriter();
  marshaller.marshal(resource, writer);

  // Compare the expected and actual outputs.
  XMLUnit.setIgnoreWhitespace(true);
  XMLUnit.setIgnoreComments(true);
  XMLUnit.setIgnoreAttributeOrder(true);
  Diff diff = new Diff(expectedXml, writer.toString());
  assertTrue("The output XML was different to the expected XML: "
    + diff.toString(), diff.identical());
}
 
Example #12
Source File: ProductResourceTest.java    From oodt with Apache License 2.0 4 votes vote down vote up
/**
 * Tests that {@link ProductResource product resources} are marshalled to
 * the expected XML format.
 * @throws IOException if the {@link Diff} constructor fails
 * @throws JAXBException if the {@link JAXBContext} or {@link Marshaller} fail
 * @throws MimeTypeException if {@link MimeTypes#forName(String)} fails
 * @throws SAXException if the {@link Diff} constructor fails
 */
@Test
public void testXmlMarshalling() throws IOException, JAXBException,
  MimeTypeException, SAXException
{
  // Create a ProductResource using ProductType, Reference, Metadata and
  // Product instances.
  Hashtable metadataEntries = new Hashtable<String, Object>();
  metadataEntries.put("CAS.Test", "test value");
  Metadata metadata = new Metadata();
  metadata.addMetadata(metadataEntries);

  Reference reference = new Reference("original", "dataStore", 1000,
    new MimeTypes().forName("text/plain"));
  List<Reference> references = new ArrayList<Reference>();
  references.add(reference);

  ProductType productType = new ProductType("1", "GenericFile", "test type",
    "repository", "versioner");

  Product product = new Product();
  product.setProductId("123");
  product.setProductName("test.txt");
  product.setProductStructure(Product.STRUCTURE_FLAT);
  product.setProductType(productType);

  ProductResource resource = new ProductResource(product, metadata,
    references, new File("/tmp"));


  // Generate the expected output.
  String expectedXml =
      "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
      + "<product>"
      + "<id>" + product.getProductId() + "</id>"
      + "<name>" + product.getProductName() + "</name>"
      + "<structure>" + product.getProductStructure() + "</structure>"
      + "<type>" + productType.getName() + "</type>"
      + "<metadata>"
      + "<keyval>"
      + "<key>" + metadata.getAllKeys().get(0) + "</key>"
      + "<val>" + metadata.getAllValues().get(0) + "</val>"
      + "</keyval>"
      + "</metadata>"
      + "<references>"
      + "<reference>"
      + "<productId>" + product.getProductId() + "</productId>"
      + "<refIndex>0</refIndex>"
      + "<dataStoreReference>"
      +    reference.getDataStoreReference()
      + "</dataStoreReference>"
      + "<originalReference>"
      +    reference.getOrigReference()
      + "</originalReference>"
      + "<mimeType>" + reference.getMimeType().getName() + "</mimeType>"
      + "<fileSize>" + reference.getFileSize() + "</fileSize>"
      + "</reference>"
      + "</references>"
      + "</product>";


  // Set up a JAXB context and marshall the ProductResource to XML.
  JAXBContext context = JAXBContext.newInstance(resource.getClass());
  Marshaller marshaller = context.createMarshaller();
  StringWriter writer = new StringWriter();
  marshaller.marshal(resource, writer);

  // Compare the expected and actual outputs.
  XMLUnit.setIgnoreWhitespace(true);
  XMLUnit.setIgnoreComments(true);
  XMLUnit.setIgnoreAttributeOrder(true);
  Diff diff = new Diff(expectedXml, writer.toString());
  assertTrue("The output XML was different to the expected XML: "
    + diff.toString(), diff.identical());
}
 
Example #13
Source File: TransferResourceTest.java    From oodt with Apache License 2.0 4 votes vote down vote up
/**
 * Tests that {@link TransferResource transfer resources} are marshalled to
 * the expected XML format.
 * @throws IOException if the {@link Diff} constructor fails
 * @throws JAXBException if the {@link JAXBContext} or {@link Marshaller} fail
 * @throws MimeTypeException if {@link MimeTypes#forName(String)} fails
 * @throws SAXException if the {@link Diff} constructor fails
 */
@Test
public void testXmlMarshalling() throws IOException, JAXBException,
  MimeTypeException, SAXException
{
  // Create a TransferResource using ProductType, Product, Metadata, Reference
  // and FileTransferStatus instances.
  Hashtable metadataEntries = new Hashtable<String, Object>();
  metadataEntries.put("CAS.ProductReceivedTime", "2013-09-12T16:25:50.662Z");
  Metadata metadata = new Metadata();
  metadata.addMetadata(metadataEntries);

  Reference reference = new Reference("original", "dataStore", 1000,
    new MimeTypes().forName("text/plain"));

  ProductType productType = new ProductType("1", "GenericFile", "test type",
    "repository", "versioner");

  Product product = new Product();
  product.setProductId("123");
  product.setProductName("test product");
  product.setProductStructure(Product.STRUCTURE_FLAT);
  product.setProductType(productType);

  FileTransferStatus status = new FileTransferStatus(reference, 1000, 100,
    product);

  TransferResource resource = new TransferResource(product, metadata, status);


  // Generate the expected output.
  String expectedXml =
    "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
    + "<transfer>"
    + "<productName>" + product.getProductName() + "</productName>"
    + "<productId>" + product.getProductId() + "</productId>"
    + "<productTypeName>" + productType.getName() + "</productTypeName>"
    + "<dataStoreReference>"
    +   reference.getDataStoreReference()
    + "</dataStoreReference>"
    + "<origReference>"
    +   reference.getOrigReference()
    + "</origReference>"
    + "<mimeType>" + reference.getMimeType().getName() + "</mimeType>"
    + "<fileSize>" + reference.getFileSize() + "</fileSize>"
    + "<totalBytes>" + reference.getFileSize() + "</totalBytes>"
    + "<bytesTransferred>"
    +    status.getBytesTransferred()
    + "</bytesTransferred>"
    + "<percentComplete>"
    +    status.computePctTransferred() * 100
    + "</percentComplete>"
    + "<productReceivedTime>"
    +    metadata.getAllValues().get(0)
    + "</productReceivedTime>"
    + "</transfer>";

  // Set up a JAXB context and marshall the DatasetResource to XML.
  JAXBContext context = JAXBContext.newInstance(resource.getClass());
  Marshaller marshaller = context.createMarshaller();
  StringWriter writer = new StringWriter();
  marshaller.marshal(resource, writer);

  // Compare the expected and actual outputs.
  XMLUnit.setIgnoreWhitespace(true);
  XMLUnit.setIgnoreComments(true);
  XMLUnit.setIgnoreAttributeOrder(true);
  Diff diff = new Diff(expectedXml, writer.toString());
  assertTrue("The output XML was different to the expected XML: "
    + diff.toString(), diff.identical());
}
 
Example #14
Source File: LocalDataTransferer.java    From oodt with Apache License 2.0 4 votes vote down vote up
/**
 * @param args
 */
public static void main(String[] args) throws DataTransferException,
      IOException, URISyntaxException {
   String usage = "LocalFileTransfer --productName <name> --productRepo <repo> [--dir <dirRef>] [--files <origRef 1>...<origRef N>]\n";

   MimeTypes mimeTypeRepo;
   try {
      mimeTypeRepo = MimeTypesFactory
            .create(System
                  .getProperty("org.apache.oodt.cas.filemgr.mime.type.repository"));
   } catch (MimeTypeException e) {
      LOG.log(Level.SEVERE, e.getMessage());
      throw new IOException(e.getMessage());
   }

   String productName = null;
   String productRepo = null;
   String transferType = null;
   Reference dirReference = null;

   List<Reference> fileReferences = null;

   for (int i = 0; i < args.length; i++) {
      if (args[i].equals("--dir")) {
         transferType = "dir";
         dirReference = new Reference();
         dirReference.setOrigReference(new File(new URI(args[++i])).toURI()
               .toString());
         LOG.log(Level.FINER,
               "LocalFileTransfer.main: Generated orig reference: "
                     + dirReference.getOrigReference());
      } else if (args[i].equals("--files")) {
         transferType = "files";
         fileReferences = new Vector<Reference>();
         for (int j = i + 1; j < args.length; j++) {
            LOG.log(Level.FINER, "LocalFileTransfer.main: Adding file ref: "
                  + args[j]);
            fileReferences.add(new Reference(args[j], null,
                  new File(args[j]).length(), mimeTypeRepo
                        .getMimeType(args[j])));
         }
      } else if (args[i].equals("--productName")) {
         productName = args[++i];
      } else if (args[i].equals("--productRepo")) {
         productRepo = args[++i];
      }
   }

   if (transferType == null || (((transferType.equals("dir") && dirReference == null) || (
       transferType.equals("files") && fileReferences == null) || (!(
       transferType.equals("dir") || transferType
           .equals("files"))) || productName == null || productRepo == null))) {
      System.err.println(usage);
      System.exit(1);
   }

   // construct a new Product
   Product p = new Product();
   p.setProductName(productName);

   if (transferType.equals("dir")) {
      p.setProductStructure(Product.STRUCTURE_HIERARCHICAL);
      dirReference.setDataStoreReference(new File(new URI(productRepo))
            .toURI().toURL().toExternalForm()
            + URLEncoder.encode(p.getProductName(), "UTF-8") + "/");
      p.getProductReferences().add(dirReference);
      /* we'll do a simple versioning scheme ourselves: no versioning! */
      p.getProductReferences().addAll(
            VersioningUtils.getReferencesFromDir(new File(new URI(
                  dirReference.getOrigReference()))));
      VersioningUtils.createBasicDataStoreRefsHierarchical(p
            .getProductReferences());
   } else if (transferType.equals("files")) {
      p.setProductStructure("Flat");
      p.getProductReferences().addAll(fileReferences);
      VersioningUtils.createBasicDataStoreRefsFlat(productName, productRepo,
            p.getProductReferences());
   }

   DataTransfer transfer = new LocalDataTransferer();
   transfer.transferProduct(p);

}
 
Example #15
Source File: SikuliService.java    From cerberus-source with GNU General Public License v3.0 4 votes vote down vote up
private JSONObject generatePostParameters(String action, String locator, String text, long defaultWait) throws JSONException, IOException, MalformedURLException, MimeTypeException {
        JSONObject result = new JSONObject();
        String picture = "";
        String extension = "";
        /**
         * Get Picture from URL and convert to Base64
         */
        if (locator != null && !"".equals(locator)) {
            URL url = new URL(locator);
            URLConnection connection = url.openConnection();

            InputStream istream = new BufferedInputStream(connection.getInputStream());

            /**
             * Get the MimeType and the extension
             */
            String mimeType = URLConnection.guessContentTypeFromStream(istream);
            MimeTypes allTypes = MimeTypes.getDefaultMimeTypes();
            MimeType mt = allTypes.forName(mimeType);
            extension = mt.getExtension();

            /**
             * Encode in Base64
             */
            byte[] bytes = IOUtils.toByteArray(istream);
            picture = Base64.encodeBase64URLSafeString(bytes);
        }
        /**
         * Build JSONObject with parameters action : Action expected to be done
         * by Sikuli picture : Picture in Base64 format text : Text to type
         * defaultWait : Timeout for the action pictureExtension : Extension for
         * Base64 decoding
         */
        result.put("action", action);
        result.put("picture", picture);
        result.put("text", text);
        result.put("defaultWait", defaultWait);
        result.put("pictureExtension", extension);
//        result.put("minSimilarity", parameterService.getParameterStringByKey("cerberus_sikuli_minSimilarity", "", null));
        return result;
    }