Example usage for java.nio.file Files probeContentType

List of usage examples for java.nio.file Files probeContentType

Introduction

In this page you can find the example usage for java.nio.file Files probeContentType.

Prototype

public static String probeContentType(Path path) throws IOException 

Source Link

Document

Probes the content type of a file.

Usage

From source file:cli.Main.java

public static void main(String[] args) {
    // Workaround for BKS truststore
    Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1);

    Namespace ns = parseArgs(args);
    if (ns == null) {
        System.exit(1);/*from ww w  . j  a v  a2s. c  o m*/
    }

    final String username = ns.getString("username");
    final Manager m = new Manager(username);
    if (m.userExists()) {
        try {
            m.load();
        } catch (Exception e) {
            System.err.println("Error loading state file \"" + m.getFileName() + "\": " + e.getMessage());
            System.exit(2);
        }
    }

    switch (ns.getString("command")) {
    case "register":
        if (!m.userHasKeys()) {
            m.createNewIdentity();
        }
        try {
            m.register(ns.getBoolean("voice"));
        } catch (IOException e) {
            System.err.println("Request verify error: " + e.getMessage());
            System.exit(3);
        }
        break;
    case "verify":
        if (!m.userHasKeys()) {
            System.err.println("User has no keys, first call register.");
            System.exit(1);
        }
        if (m.isRegistered()) {
            System.err.println("User registration is already verified");
            System.exit(1);
        }
        try {
            m.verifyAccount(ns.getString("verificationCode"));
        } catch (IOException e) {
            System.err.println("Verify error: " + e.getMessage());
            System.exit(3);
        }
        break;
    case "send":
        if (!m.isRegistered()) {
            System.err.println("User is not registered.");
            System.exit(1);
        }
        String messageText = ns.getString("message");
        if (messageText == null) {
            try {
                messageText = IOUtils.toString(System.in);
            } catch (IOException e) {
                System.err.println("Failed to read message from stdin: " + e.getMessage());
                System.exit(1);
            }
        }

        final List<String> attachments = ns.getList("attachment");
        List<TextSecureAttachment> textSecureAttachments = null;
        if (attachments != null) {
            textSecureAttachments = new ArrayList<>(attachments.size());
            for (String attachment : attachments) {
                try {
                    File attachmentFile = new File(attachment);
                    InputStream attachmentStream = new FileInputStream(attachmentFile);
                    final long attachmentSize = attachmentFile.length();
                    String mime = Files.probeContentType(Paths.get(attachment));
                    textSecureAttachments
                            .add(new TextSecureAttachmentStream(attachmentStream, mime, attachmentSize, null));
                } catch (IOException e) {
                    System.err.println("Failed to add attachment \"" + attachment + "\": " + e.getMessage());
                    System.err.println("Aborting sending.");
                    System.exit(1);
                }
            }
        }

        List<TextSecureAddress> recipients = new ArrayList<>(ns.<String>getList("recipient").size());
        for (String recipient : ns.<String>getList("recipient")) {
            try {
                recipients.add(m.getPushAddress(recipient));
            } catch (InvalidNumberException e) {
                System.err.println("Failed to add recipient \"" + recipient + "\": " + e.getMessage());
                System.err.println("Aborting sending.");
                System.exit(1);
            }
        }
        sendMessage(m, messageText, textSecureAttachments, recipients);
        break;
    case "receive":
        if (!m.isRegistered()) {
            System.err.println("User is not registered.");
            System.exit(1);
        }
        try {
            m.receiveMessages(5, true, new ReceiveMessageHandler(m));
        } catch (IOException e) {
            System.err.println("Error while receiving message: " + e.getMessage());
            System.exit(3);
        } catch (AssertionError e) {
            System.err.println("Failed to receive message (Assertion): " + e.getMessage());
            System.err.println(e.getStackTrace());
            System.err.println(
                    "If you use an Oracle JRE please check if you have unlimited strength crypto enabled, see README");
            System.exit(1);
        }
        break;
    }
    m.save();
    System.exit(0);
}

From source file:Test.java

  static void displayContentType(String pathText) throws Exception {
  Path path = Paths.get(pathText);
  String type = Files.probeContentType(path);
  System.out.println(type);/*  w w w. j  a v  a 2 s .  c om*/
}

From source file:Main.java

static void displayContentType(String pathText) throws Exception {
    Path path = Paths.get(pathText);
    String type = Files.probeContentType(path);
    System.out.println(type);//from   w  w w  . j a  va  2  s.com
}

From source file:base64test.Base64Test.java

public static String getFileType(Path filePath) {
    String fileType = "Undetermined";
    try {//from  w w  w  .j av a 2s . c  om
        fileType = Files.probeContentType(filePath);
    } catch (IOException ioE) {
        System.err.println(
                "ERROR: Unable to determine file type for " + filePath.toString() + " due to exception " + ioE);
    }

    return fileType;
}

From source file:com.swingtech.apps.filemgmt.util.MimeTypeUtils.java

public static String probeContentType(Path file) throws IOException {
    String mimetype = Files.probeContentType(file);
    if (mimetype != null)
        return mimetype;

    mimetype = tika.detect(file.toFile());
    if (mimetype != null)
        return mimetype;

    return getMimeType(FilenameUtils.getExtension(String.valueOf(file.getFileName())));
}

From source file:com.exsoloscript.challonge.model.query.AttachmentQuery.java

public String getMimeType() throws IOException {
    Validate.notNull(asset, "Cannot get MIME type since asset is null");
    return Files.probeContentType(asset.toPath());
}

From source file:edu.jhu.hlt.concrete.ingesters.alnc.ALNCIngester.java

public ALNCIngester(Path path) throws IngestException {
    this.ts = Timing.currentLocalTime();
    this.path = path;
    try {/*from   ww  w.  j  ava2 s. c  o m*/
        Optional<String> inferredType = Optional.ofNullable(Files.probeContentType(this.path));
        String ft = inferredType.orElse("unk");
        if (ft.contains("bzip"))
            this.conv = new ALNCFileConverter(new BZip2CompressorInputStream(Files.newInputStream(this.path)));
        else
            this.conv = new ALNCFileConverter(Files.newInputStream(this.path));
    } catch (IOException e) {
        throw new IngestException(e);
    }
}

From source file:com.adaptris.core.services.ReadFileServiceTest.java

@Test
public void testProbeContentType() throws Exception {
    final ReadFileService service = new ReadFileService();
    assertNotNull(service.contentTypeProbe());
    ReadFileService.ContentTypeProbe myProbe = e -> {
        return Files.probeContentType(e.toPath());
    };//from  w ww  . ja  va2 s .c  o  m
    service.setContentTypeProbe(myProbe);
    assertEquals(myProbe, service.getContentTypeProbe());
    assertEquals(myProbe, service.contentTypeProbe());
}

From source file:snow.http.server.ResourceHttpHandler.java

public static String mimeType(Path path) throws IOException {
    String mime = Files.probeContentType(path);
    if (mime == null)
        mime = getDefaultFileTypeMap().getContentType(path.toString());
    return mime;/*from  ww w  .  java 2 s .c  o  m*/
}

From source file:org.dynamicloud.wiztorage.util.WiztorageUtils.java

/**
 * This methods returns a type according to the file
 *
 * @param file in context//from   w ww  .j a  va  2s.c om
 * @return the file type
 */
public static String getType(File file) {
    try {
        String contentType = Files.probeContentType(Paths.get(file.getAbsolutePath()));
        if (contentType != null) {
            return contentType;
        }
    } catch (Exception ignore) {

    }

    String fileName = file.getName();

    int lastDot = fileName.lastIndexOf(".");
    if (lastDot < 0) {
        return "Unrecognized";
    }

    return fileName.substring(lastDot + 1) + "/file";
}