Example usage for javax.activation MimetypesFileTypeMap MimetypesFileTypeMap

List of usage examples for javax.activation MimetypesFileTypeMap MimetypesFileTypeMap

Introduction

In this page you can find the example usage for javax.activation MimetypesFileTypeMap MimetypesFileTypeMap.

Prototype

public MimetypesFileTypeMap() 

Source Link

Document

The default constructor.

Usage

From source file:net.bican.wordpress.Main.java

/**
 * @param args execute with "-?" for an explanation of args
 * @throws ParseException When the command line options cannot be parsed
 *//*from  w  w  w . ja  v a  2 s.  c  o  m*/
public static void main(String[] args) throws ParseException {
    try {
        Options options = new Options();
        options.addOption("?", "help", false, "Print usage information");
        options.addOption("h", "url", true, "Specify the url to xmlrpc.php");
        options.addOption("u", "user", true, "User name");
        options.addOption("p", "pass", true, "Password");
        options.addOption("a", "authors", false, "Get author list");
        options.addOption("s", "slug", true, "Slug for categories");
        options.addOption("pi", "parentid", true, "Parent id for categories");
        options.addOption("oi", "postid", true, "Post id for pages and posts");
        options.addOption("c", "categories", false, "Get category list");
        options.addOption("cn", "newcategory", true, "New category (uses --slug and --parentid)");
        options.addOption("pg", "pages", false, "Get page list (full)");
        options.addOption("pl", "pagelist", false, "Get page list");
        options.addOption("ps", "page", true, "Get page");
        options.addOption("pn", "newpage", true, "New page from file <arg> (needs --publish)");
        options.addOption("pe", "editpage", true, "Edit page (needs --postid and --publish");
        options.addOption("pd", "deletepage", true, "Delete page (needs --publish)");
        options.addOption("l", "publish", true, "Publish status for \"new\" options");
        options.addOption("us", "userinfo", false, "Get user information");
        options.addOption("or", "recentposts", true, "Get recent posts");
        options.addOption("os", "getpost", true, "Get post");
        options.addOption("on", "newpost", true, "New post from file <arg> (needs --publish)");
        options.addOption("oe", "editpost", true, "Edit post (needs --postid and --publish");
        options.addOption("od", "deletepost", true, "Delete post (needs --publish)");
        options.addOption("sm", "supportedmethods", false, "List supported methods");
        options.addOption("st", "supportedfilters", false, "List supported text filters");
        options.addOption("mn", "newmedia", true, "New media file (uses --overwrite)");
        options.addOption("ov", "overwrite", false, "Allow overwrite in uploading new media");
        options.addOption("so", "supportedstatus", false, "Print supported page and post status values");
        options.addOption("cs", "commentstatus", false, "Print comment status names for the blog");
        options.addOption("cc", "commentcount", true, "Get comment count for a post (-1 for all posts)");
        options.addOption("ca", "newcomment", true, "New comment from file");
        options.addOption("cd", "deletecomment", true, "Delete comment");
        options.addOption("ce", "editcomment", true, "Edit comment from file");
        options.addOption("cg", "getcomment", true, "Get comment");
        options.addOption("ct", "getcomments", true, "Get comments for the post");
        options.addOption("cs", "commentstatus", true, "Comment status (for --getcomments)");
        options.addOption("co", "commentoffset", true, "Comment offset # (for --getcomments)");
        options.addOption("cm", "commentnumber", true, "Comment # (for --getcomments)");
        try {
            WpCliConfiguration config = new WpCliConfiguration(args, options, Main.class);
            if (config.hasOption("help")) {
                showHelp(options);
            } else if ((!config.hasOption("url")) || (!config.hasOption("user"))
                    || (!config.hasOption("pass"))) {
                System.err.println("Specify --user, --pass and --url");
            } else {
                try {
                    Wordpress wp = new Wordpress(config.getOptionValue("user"), config.getOptionValue("pass"),
                            config.getOptionValue("url"));
                    if (config.hasOption("authors")) {
                        printList(wp.getAuthors(), Author.class, true);
                    } else if (config.hasOption("categories")) {
                        printList(wp.getCategories(), Category.class, true);
                    } else if (config.hasOption("newcategory")) {
                        String slug = config.getOptionValue("slug");
                        Integer parentId = getInteger("parentid", config);
                        if (slug == null)
                            slug = "";
                        if (parentId == null)
                            parentId = 0;
                        System.out
                                .println(wp.newCategory(config.getOptionValue("newcategory"), slug, parentId));
                    } else if (config.hasOption("pages")) {
                        printList(wp.getPages(), Page.class, false);
                    } else if (config.hasOption("pagelist")) {
                        printList(wp.getPageList(), PageDefinition.class, false);
                    } else if (config.hasOption("page")) {
                        printItem(wp.getPage(getInteger("page", config)), Page.class);
                    } else if (config.hasOption("userinfo")) {
                        printItem(wp.getUserInfo(), User.class);
                    } else if (config.hasOption("recentposts")) {
                        printList(wp.getRecentPosts(getInteger("recentposts", config)), Page.class, false);
                    } else if (config.hasOption("getpost")) {
                        printItem(wp.getPost(getInteger("getpost", config)), Page.class);
                    } else if (config.hasOption("supportedmethods")) {
                        printList(wp.supportedMethods(), String.class, false);
                    } else if (config.hasOption("supportedfilters")) {
                        printList(wp.supportedTextFilters(), String.class, false);
                    } else if (config.hasOption("newpage")) {
                        if (!config.hasOption("publish")) {
                            showHelp(options);
                        } else {
                            System.out.println(
                                    wp.newPage(Page.fromFile(new File(config.getOptionValue("newpage"))),
                                            config.getOptionValue("publish")));
                        }
                    } else if (config.hasOption("editpage")) {
                        edit(options, config, wp, "editpage", true);
                    } else if (config.hasOption("editpost")) {
                        edit(options, config, wp, "editpost", false);
                    } else if (config.hasOption("deletepage")) {
                        delete(options, config, wp, "deletepage", true);
                    } else if (config.hasOption("deletepost")) {
                        delete(options, config, wp, "deletepost", false);
                    } else if (config.hasOption("newpost")) {
                        if (!config.hasOption("publish")) {
                            showHelp(options);
                        } else {
                            System.out.println(
                                    wp.newPost(Page.fromFile(new File(config.getOptionValue("newpost"))),
                                            Boolean.valueOf(config.getOptionValue("publish"))));
                        }
                    } else if (config.hasOption("newmedia")) {
                        String fileName = config.getOptionValue("newmedia");
                        File file = new File(fileName);
                        String mimeType = new MimetypesFileTypeMap().getContentType(file);
                        Boolean overwrite = Boolean.FALSE;
                        if (config.hasOption("overwrite"))
                            overwrite = Boolean.TRUE;
                        MediaObject result = wp.newMediaObject(mimeType, file, overwrite);
                        if (result != null) {
                            System.out.println(result);
                        }
                    } else if (config.hasOption("supportedstatus")) {
                        System.out.println("Recognized status values for posts:");
                        printList(wp.getPostStatusList(), PostAndPageStatus.class, true);
                        System.out.println("\nRecognized status values for pages:");
                        printList(wp.getPageStatusList(), PostAndPageStatus.class, true);
                    } else if (config.hasOption("commentstatus")) {
                        showCommentStatus(wp);
                    } else if (config.hasOption("commentcount")) {
                        showCommentCount(config, wp);
                    } else if (config.hasOption("newcomment")) {
                        editComment(wp, config.getOptionValue("newcomment"), "newcomment");
                    } else if (config.hasOption("editcomment")) {
                        editComment(wp, config.getOptionValue("editcomment"), "editcomment");
                    } else if (config.hasOption("deletecomment")) {
                        System.err.println(Integer.valueOf(config.getOptionValue("deletecomment")));
                        deleteComment(wp, Integer.valueOf(config.getOptionValue("deletecomment")));
                    } else if (config.hasOption("getcomment")) {
                        printComment(wp, Integer.valueOf(config.getOptionValue("getcomment")));
                    } else if (config.hasOption("getcomments")) {
                        Integer postID = Integer.valueOf(config.getOptionValue("getcomments"));
                        String commentStatus = config.getOptionValue("commentstatus");
                        Integer commentOffset;
                        try {
                            commentOffset = Integer.valueOf(config.getOptionValue("commentoffset"));
                        } catch (NumberFormatException e) {
                            commentOffset = null;
                        }
                        Integer commentNumber;
                        try {
                            commentNumber = Integer.valueOf(config.getOptionValue("commentnumber"));
                        } catch (Exception e) {
                            commentNumber = null;
                        }
                        printComments(wp, postID, commentStatus, commentOffset, commentNumber);
                    } else {
                        showHelp(options);
                    }
                } catch (MalformedURLException e) {
                    System.err.println("URL \"" + config.getOptionValue("url") + "\" is invalid, reason is: "
                            + e.getLocalizedMessage());
                } catch (IOException e) {
                    System.err.println("Can't read from file, reason is: " + e.getLocalizedMessage());
                } catch (InvalidPostFormatException e) {
                    System.err.println("Input format is invalid.");
                }
            }
        } catch (ParseException e) {
            System.err.println("Can't process command line arguments, reason is: " + e.getLocalizedMessage());
        }
    } catch (XmlRpcFault e) {
        String reason = e.getLocalizedMessage();
        System.err.println("Operation failed, reason is: " + reason);
    }
}

From source file:io.jeffrey.web.assemble.InMemoryAssembler.java

private static String getContentType(File source, String key) {
    String[] parts = key.split(Pattern.quote("."));
    if (parts.length > 0) {
        String ext = parts[parts.length - 1].toLowerCase();
        if ("png".equals(ext))
            return "image/png";
        if ("css".equals(ext))
            return "text/css";
    }//from w  w  w  .  j a v a 2s  .  co m
    return new MimetypesFileTypeMap().getContentType(source);
}

From source file:be.fedict.eid.dss.spi.MimeTypeMapper.java

public static boolean browserViewable(final List<MimeType> mimeTypes, final String fileName) {

    String mimeTypeString = new MimetypesFileTypeMap().getContentType(fileName);
    LOG.debug("File " + fileName + " -> mimeType: " + mimeTypeString);

    if (mimeTypeString.toLowerCase().startsWith("image/")) {
        return true;
    }// w w w.j  a  v  a  2  s .  c om
    for (MimeType mimeType : mimeTypes) {
        if (mimeType.getType().toLowerCase().startsWith(mimeTypeString.toLowerCase())) {
            return true;
        }
    }

    return false;
}

From source file:org.userscripts.justenwalker.GmResourceGenerator.java

public static String fileToDataUrl(File file) {
    try {//from ww w.  j  a va  2  s .  com
        // Get File's MIME Type
        String mime = new MimetypesFileTypeMap().getContentType(file);

        // Create URL-Safe Base64 Encoded Link
        byte[] fileBytes = readFileBytes(file);
        String b64 = URLEncoder.encode(Base64.encodeBase64String(fileBytes), "utf-8");
        return ("data:" + mime + ";base64," + b64);
    } catch (IOException e) {
        System.err.println("Could not read file " + file.getName());
        System.err.println(e.toString());
    }
    return "";
}

From source file:com.playonlinux.core.utils.FileAnalyser.java

public static String getMimetype(File inputFile) throws PlayOnLinuxException {
    try {/*from w ww.j a v a 2 s .c  o m*/
        return getMatch(inputFile).getMimeType();
    } catch (MagicMatchNotFoundException e) {
        LOGGER.debug(e);
        final MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
        return mimeTypesMap.getContentType(inputFile);
    }
}

From source file:org.silverpeas.mobile.server.helpers.DataURLHelper.java

/**
 * Return avatar dataurl/* ww  w.  j a  v a2  s  .co m*/
 * @param photoFileName
 * @return
 */
public static String convertAvatarToUrlData(String photoFileName, String size) {

    int i = photoFileName.lastIndexOf("/");
    if (i == -1)
        i = photoFileName.lastIndexOf("\\");
    if (i != -1) {
        photoFileName = photoFileName.substring(i + 1);
    }

    String data = "";
    try {
        File originalImage = new File(
                FileRepositoryManager.getAvatarPath() + File.separatorChar + photoFileName);
        if (!originalImage.exists()) {
            return "";
        }

        String askedPath = originalImage.getParent() + File.separator + size + File.separator + photoFileName;
        SilverpeasFile image = SilverpeasFileProvider.getFile(askedPath);

        byte[] binaryData = IOUtils.toByteArray(image.inputStream());
        MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
        data = "data:" + mimeTypesMap.getContentType(originalImage) + ";base64,"
                + new String(Base64.encodeBase64(binaryData));
    } catch (Exception e) {
        SilverLogger.getLogger(SpMobileLogModule.getName())
                .error("PublicationContentServlet.convertSpImageUrlToDataUrl", "root.EX_NO_MESSAGE", e);
    }
    return data;
}

From source file:ddf.catalog.data.BinaryContentImplTest.java

@Before
public void setUp() {
    content = new File("src/test/resources/data/i4ce.png");
    MimetypesFileTypeMap mimeMapper = new MimetypesFileTypeMap();
    try {/*from w ww  . ja v a2s .  co m*/
        mimeType = new MimeType(mimeMapper.getContentType(content));
    } catch (MimeTypeParseException e) {
        LOGGER.error("Mime parser Failure", e);
        new Failure(null, e);
    }
}

From source file:org.estatio.dscm.fixture.asset.AssetAbstract.java

private Blob resourceAsBlob(String fileName) {
    try {//from  w w  w  .ja  va2s .c  om
        InputStream is;
        is = getClass().getResourceAsStream("/" + fileName);
        final String mimeType = new MimetypesFileTypeMap().getContentType(fileName);
        Blob blob = new Blob(fileName, mimeType, IOUtils.toByteArray(is));
        is.close();
        return blob;
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;

}

From source file:com.elasticgrid.storage.rackspace.CloudFilesContainer.java

public CloudFilesContainer(final FilesClient rackspace, final FilesContainer rackspaceContainer)
        throws NoSuchFieldException {
    this.rackspace = rackspace;
    this.rackspaceContainer = rackspaceContainer;
    this.mimes = new MimetypesFileTypeMap();
    mimeTypeField = FilesObject.class.getDeclaredField("mimeType");
    mimeTypeField.setAccessible(true);/*from   w w w.  j a  v  a  2  s.  co  m*/
}

From source file:annis.administration.MediaImportHelper.java

public MediaImportHelper(String absolutePath, File dataDir, long corpusRef,
        Map<String, String> mimeTypeMapping) {

    this.fileSource = new File(absolutePath);

    // create a file-name in the form of "filename-UUID.ending", thus we
    // need to split the file name into its components
    String baseName = FilenameUtils.getBaseName(fileSource.getName());
    String extension = FilenameUtils.getExtension(fileSource.getName());
    UUID uuid = UUID.randomUUID();
    fileDestination = new File(dataDir,
            baseName + "_" + uuid.toString() + (extension.isEmpty() ? "" : "." + extension));

    String fileEnding = FilenameUtils.getExtension(absolutePath);
    if (mimeTypeMapping.containsKey(fileEnding)) {
        this.mimeType = mimeTypeMapping.get(fileEnding);
    } else {//from w w w  .  java2  s  . c o  m
        this.mimeType = new MimetypesFileTypeMap().getContentType(fileSource);
    }
    this.corpusRef = corpusRef;

}