Example usage for java.io File canRead

List of usage examples for java.io File canRead

Introduction

In this page you can find the example usage for java.io File canRead.

Prototype

public boolean canRead() 

Source Link

Document

Tests whether the application can read the file denoted by this abstract pathname.

Usage

From source file:com.openkm.util.impexp.RepositoryImporter.java

/**
 * Import mail.//from  ww  w.j  a v  a  2  s  .c o  m
 */
private static ImpExpStats importMail(String token, String fldPath, String fileName, File fDoc,
        boolean metadata, Writer out, InfoDecorator deco) throws IOException, PathNotFoundException,
        AccessDeniedException, RepositoryException, DatabaseException, ExtensionException, AutomationException {
    FileInputStream fisContent = new FileInputStream(fDoc);
    MetadataAdapter ma = MetadataAdapter.getInstance(token);
    MailModule mm = ModuleManager.getMailModule();
    Properties props = System.getProperties();
    props.put("mail.host", "smtp.dummydomain.com");
    props.put("mail.transport.protocol", "smtp");
    ImpExpStats stats = new ImpExpStats();
    int size = fisContent.available();
    Mail mail = new Mail();
    Gson gson = new Gson();
    boolean api = false;

    try {
        // Metadata
        if (metadata) {
            // Read serialized document metadata
            File jsFile = new File(fDoc.getPath() + Config.EXPORT_METADATA_EXT);
            log.info("Document Metadata File: {}", jsFile.getPath());

            if (jsFile.exists() && jsFile.canRead()) {
                FileReader fr = new FileReader(jsFile);
                MailMetadata mmd = gson.fromJson(fr, MailMetadata.class);
                mail.setPath(fldPath + "/" + fileName);
                mmd.setPath(mail.getPath());
                IOUtils.closeQuietly(fr);
                log.info("Mail Metadata: {}", mmd);

                // Apply metadata
                ma.importWithMetadata(mmd);

                // Add attachments
                if (fileName.endsWith(".eml")) {
                    Session mailSession = Session.getDefaultInstance(props, null);
                    MimeMessage msg = new MimeMessage(mailSession, fisContent);
                    mail = MailUtils.messageToMail(msg);
                    mail.setPath(fldPath + "/" + mmd.getName());
                    MailUtils.addAttachments(null, mail, msg, PrincipalUtils.getUser());
                } else if (fileName.endsWith(".msg")) {
                    Message msg = new MsgParser().parseMsg(fisContent);
                    mail = MailUtils.messageToMail(msg);
                    mail.setPath(fldPath + "/" + mmd.getName());
                    MailUtils.addAttachments(null, mail, msg, PrincipalUtils.getUser());
                } else {
                    throw new MessagingException("Unknown mail format");
                }

                FileLogger.info(BASE_NAME, "Created document ''{0}''", mail.getPath());
                log.info("Created document '{}'", mail.getPath());
            } else {
                log.warn("Unable to read metadata file: {}", jsFile.getPath());
                api = true;
            }
        } else {
            api = true;
        }

        if (api) {
            if (fileName.endsWith(".eml")) {
                Session mailSession = Session.getDefaultInstance(props, null);
                MimeMessage msg = new MimeMessage(mailSession, fisContent);
                mail = MailUtils.messageToMail(msg);
                mail.setPath(fldPath + "/" + UUID.randomUUID().toString() + "-"
                        + PathUtils.escape(mail.getSubject()));
                mm.create(token, mail);
                MailUtils.addAttachments(null, mail, msg, PrincipalUtils.getUser());
            } else if (fileName.endsWith(".msg")) {
                Message msg = new MsgParser().parseMsg(fisContent);
                mail = MailUtils.messageToMail(msg);
                mail.setPath(fldPath + "/" + UUID.randomUUID().toString() + "-"
                        + PathUtils.escape(mail.getSubject()));
                mm.create(token, mail);
                MailUtils.addAttachments(null, mail, msg, PrincipalUtils.getUser());
            } else {
                throw new MessagingException("Unknown mail format");
            }

            FileLogger.info(BASE_NAME, "Created mail ''{0}''", mail.getPath());
            log.info("Created mail ''{}''", mail.getPath());
        }

        if (out != null) {
            out.write(deco.print(fDoc.getPath(), fDoc.length(), null));
            out.flush();
        }

        // Stats
        stats.setSize(stats.getSize() + size);
        stats.setMails(stats.getMails() + 1);
    } catch (UnsupportedMimeTypeException e) {
        log.warn("UnsupportedMimeTypeException: {}", e.getMessage());

        if (out != null) {
            out.write(deco.print(fDoc.getPath(), fDoc.length(), "UnsupportedMimeType"));
            out.flush();
        }

        stats.setOk(false);
        FileLogger.error(BASE_NAME, "UnsupportedMimeTypeException ''{0}''", mail.getPath());
    } catch (FileSizeExceededException e) {
        log.warn("FileSizeExceededException: {}", e.getMessage());

        if (out != null) {
            out.write(deco.print(fDoc.getPath(), fDoc.length(), "FileSizeExceeded"));
            out.flush();
        }

        stats.setOk(false);
        FileLogger.error(BASE_NAME, "FileSizeExceededException ''{0}''", mail.getPath());
    } catch (UserQuotaExceededException e) {
        log.warn("UserQuotaExceededException: {}", e.getMessage());

        if (out != null) {
            out.write(deco.print(fDoc.getPath(), fDoc.length(), "UserQuotaExceeded"));
            out.flush();
        }

        stats.setOk(false);
        FileLogger.error(BASE_NAME, "UserQuotaExceededException ''{0}''", mail.getPath());
    } catch (VirusDetectedException e) {
        log.warn("VirusWarningException: {}", e.getMessage());

        if (out != null) {
            out.write(deco.print(fDoc.getPath(), fDoc.length(), "VirusWarningException"));
            out.flush();
        }

        stats.setOk(false);
        FileLogger.error(BASE_NAME, "VirusWarningException ''{0}''", mail.getPath());
    } catch (ItemExistsException e) {
        log.warn("ItemExistsException: {}", e.getMessage());

        if (out != null) {
            out.write(deco.print(fDoc.getPath(), fDoc.length(), "ItemExists"));
            out.flush();
        }

        stats.setOk(false);
        FileLogger.error(BASE_NAME, "ItemExistsException ''{0}''", mail.getPath());
    } catch (JsonParseException e) {
        log.warn("JsonParseException: {}", e.getMessage());

        if (out != null) {
            out.write(deco.print(fDoc.getPath(), fDoc.length(), "Json"));
            out.flush();
        }

        stats.setOk(false);
        FileLogger.error(BASE_NAME, "JsonParseException ''{0}''", mail.getPath());
    } catch (MessagingException e) {
        log.warn("MessagingException: {}", e.getMessage());

        if (out != null) {
            out.write(deco.print(fDoc.getPath(), fDoc.length(), "Messaging"));
            out.flush();
        }

        stats.setOk(false);
        FileLogger.error(BASE_NAME, "MessagingException ''{0}''", mail.getPath());
    } catch (Exception e) {
        log.warn("Exception: {}", e.getMessage());

        if (out != null) {
            out.write(deco.print(fDoc.getPath(), fDoc.length(), "General"));
            out.flush();
        }

        stats.setOk(false);
        FileLogger.error(BASE_NAME, "Exception ''{0}''", mail.getPath());
    } finally {
        IOUtils.closeQuietly(fisContent);
    }

    return stats;
}

From source file:PerformanceQueues_p.java

public static serverObjects respond(final RequestHeader header, final serverObjects post,
        final serverSwitch env) {
    // return variable that accumulates replacements
    final Switchboard sb = (Switchboard) env;
    final serverObjects prop = new serverObjects();
    File defaultSettingsFile = new File(sb.getAppPath(), "defaults/yacy.init");

    /* Acquire a transaction token for the next POST form submission */
    prop.put(TransactionManager.TRANSACTION_TOKEN_PARAM, TransactionManager.getTransactionToken(header));

    // get segment
    Segment indexSegment = sb.index;

    if (post != null) {
        /* Check the transaction is valid : validation apply then for every uses of this post parameter */
        TransactionManager.checkPostTransaction(header, post);

        if (post.containsKey("resetObserver")) {
            /* The the reset state button is pushed, we only perform this action and do not save other form field values at the same time */
            MemoryControl.resetProperState();
        } else {// w ww .  j ava  2 s  .  c o  m
            if (post.containsKey("defaultFile")) {
                // TODO check file-path!
                final File value = new File(sb.getAppPath(), post.get("defaultFile", "defaults/yacy.init"));
                // check if value is readable file
                if (value.exists() && value.isFile() && value.canRead()) {
                    defaultSettingsFile = value;
                }
            }
            if (post.containsKey("Xmx")) {
                int xmx = post.getInt("Xmx", 600); // default maximum heap size
                if (OS.isWin32)
                    xmx = Math.min(2000, xmx);
                int xms = xmx; //Math.min(xmx, Math.max(90, xmx / 10));
                sb.setConfig("javastart_Xmx", "Xmx" + xmx + "m");
                sb.setConfig("javastart_Xms", "Xms" + xms + "m");
                prop.put("setStartupCommit", "1");

                /* Acquire a transaction token for the restart operation */
                prop.put("setStartupCommit_" + TransactionManager.TRANSACTION_TOKEN_PARAM,
                        TransactionManager.getTransactionToken(header, "/Steering.html"));
            }
            if (post.containsKey("diskFree")) {
                sb.setConfig(SwitchboardConstants.RESOURCE_DISK_FREE_MIN_STEADYSTATE, post.getLong("diskFree",
                        SwitchboardConstants.RESOURCE_DISK_FREE_MIN_STEADYSTATE_DEFAULT));
            }
            if (post.containsKey("diskFreeHardlimit")) {
                sb.setConfig(SwitchboardConstants.RESOURCE_DISK_FREE_MIN_UNDERSHOT, post.getLong(
                        "diskFreeHardlimit", SwitchboardConstants.RESOURCE_DISK_FREE_MIN_UNDERSHOT_DEFAULT));

                /* This is a checkbox in Performance_p.html : when not checked the value is not in post parameters, 
                 * so we take only in account when the relate diskFreeHardlimit is set */
                sb.setConfig(SwitchboardConstants.RESOURCE_DISK_FREE_AUTOREGULATE,
                        post.getBoolean("diskFreeAutoregulate"));
            }
            if (post.containsKey("diskUsed")) {
                sb.setConfig(SwitchboardConstants.RESOURCE_DISK_USED_MAX_STEADYSTATE, post.getLong("diskUsed",
                        SwitchboardConstants.RESOURCE_DISK_USED_MAX_STEADYSTATE_DEFAULT));
            }
            if (post.containsKey("diskUsedHardlimit")) {
                sb.setConfig(SwitchboardConstants.RESOURCE_DISK_USED_MAX_OVERSHOT, post.getLong(
                        "diskUsedHardlimit", SwitchboardConstants.RESOURCE_DISK_USED_MAX_OVERSHOT_DEFAULT));

                /* This is a checkbox in Performance_p.html : when not checked the value is not in post parameters, 
                 * so we take only in account when the related diskFreeHardlimit is set */
                sb.setConfig(SwitchboardConstants.RESOURCE_DISK_USED_AUTOREGULATE,
                        post.getBoolean("diskUsedAutoregulate"));
            }
            if (post.containsKey("memoryAcceptDHT")) {
                sb.setConfig(SwitchboardConstants.MEMORY_ACCEPTDHT, post.getInt("memoryAcceptDHT", 50));
            }
        }
    }
    final Map<String, String> defaultSettings = ((post == null) || (!(post.containsKey("submitdefault"))))
            ? null
            : FileUtils.loadMap(defaultSettingsFile);
    Iterator<String> threads = sb.threadNames();
    String threadName;
    BusyThread thread;

    final boolean xml = header.getPathInfo().endsWith(".xml");
    prop.setLocalized(!xml);

    // calculate totals
    long blocktime_total = 0, sleeptime_total = 0, exectime_total = 0;
    while (threads.hasNext()) {
        threadName = threads.next();
        thread = sb.getThread(threadName);
        blocktime_total += thread.getBlockTime();
        sleeptime_total += thread.getSleepTime();
        exectime_total += thread.getExecTime();
    }
    if (blocktime_total == 0)
        blocktime_total = 1;
    if (sleeptime_total == 0)
        sleeptime_total = 1;
    if (exectime_total == 0)
        exectime_total = 1;

    // set templates for latest news from the threads
    long blocktime, sleeptime, exectime;
    long idlesleep, busysleep, memuse, memprereq;
    double loadprereq;
    int queuesize;
    threads = sb.threadNames();
    int c = 0;
    long idleCycles, busyCycles, memshortageCycles, highCPUCycles;
    // set profile?
    final boolean setProfile = (post != null && post.containsKey("submitdefault"));
    final boolean setDelay = (post != null) && (post.containsKey("submitdelay"));
    // save used settings file to config
    if (setProfile && post != null) {
        sb.setConfig("performanceSpeed", post.getInt("profileSpeed", 100));
    }

    IndexCell<WordReference> rwi = indexSegment.termIndex();
    while (threads.hasNext()) {
        threadName = threads.next();
        thread = sb.getThread(threadName);

        // set values to templates
        prop.put("table_" + c + "_threadname", threadName);

        prop.putHTML("table_" + c + "_hasurl_shortdescr", thread.getShortDescription());
        if (thread.getMonitorURL() == null) {
            prop.put("table_" + c + "_hasurl", "0");
        } else {
            prop.put("table_" + c + "_hasurl", "1");
            prop.put("table_" + c + "_hasurl_url", thread.getMonitorURL());
        }
        prop.putHTML("table_" + c + "_longdescr", thread.getLongDescription());
        queuesize = thread.getJobCount();
        prop.put("table_" + c + "_queuesize",
                (queuesize == Integer.MAX_VALUE) ? "unlimited" : Formatter.number(queuesize, !xml));

        blocktime = thread.getBlockTime();
        sleeptime = thread.getSleepTime();
        exectime = thread.getExecTime();
        memuse = thread.getMemoryUse();
        idleCycles = thread.getIdleCycles();
        busyCycles = thread.getBusyCycles();
        memshortageCycles = thread.getOutOfMemoryCycles();
        highCPUCycles = thread.getHighCPUCycles();
        prop.putNum("table_" + c + "_blocktime", blocktime / 1000);
        prop.putNum("table_" + c + "_blockpercent", 100 * blocktime / blocktime_total);
        prop.putNum("table_" + c + "_sleeptime", sleeptime / 1000);
        prop.putNum("table_" + c + "_sleeppercent", 100 * sleeptime / sleeptime_total);
        prop.putNum("table_" + c + "_exectime", exectime / 1000);
        prop.putNum("table_" + c + "_execpercent", 100 * exectime / exectime_total);
        prop.putNum("table_" + c + "_totalcycles", idleCycles + busyCycles + memshortageCycles + highCPUCycles);
        prop.putNum("table_" + c + "_idlecycles", idleCycles);
        prop.putNum("table_" + c + "_busycycles", busyCycles);
        prop.putNum("table_" + c + "_memscycles", memshortageCycles);
        prop.putNum("table_" + c + "_highcpucycles", highCPUCycles);
        prop.putNum("table_" + c + "_sleeppercycle",
                ((idleCycles + busyCycles) == 0) ? -1 : sleeptime / (idleCycles + busyCycles));
        prop.putNum("table_" + c + "_execpercycle", (busyCycles == 0) ? -1 : exectime / busyCycles);
        prop.putNum("table_" + c + "_memusepercycle", (busyCycles == 0) ? -1 : memuse / busyCycles / 1024);

        // load with old values
        idlesleep = sb.getConfigLong(threadName + "_idlesleep", 1000);
        busysleep = sb.getConfigLong(threadName + "_busysleep", 100);
        memprereq = sb.getConfigLong(threadName + "_memprereq", 0);
        loadprereq = sb.getConfigFloat(threadName + "_loadprereq", 9);
        if (setDelay && post != null) {
            // load with new values
            idlesleep = post.getLong(threadName + "_idlesleep", idlesleep);
            busysleep = post.getLong(threadName + "_busysleep", busysleep);
            memprereq = post.getLong(threadName + "_memprereq", memprereq) * 1024l;
            if (memprereq == 0)
                memprereq = sb.getConfigLong(threadName + "_memprereq", 0);
            loadprereq = post.getDouble(threadName + "_loadprereq", loadprereq);
            if (loadprereq == 0)
                loadprereq = sb.getConfigFloat(threadName + "_loadprereq", 9);

            // check values to prevent short-cut loops
            if (idlesleep < 1000)
                idlesleep = 1000;

            sb.setThreadPerformance(threadName, idlesleep, busysleep, memprereq, loadprereq);
            idlesleep = sb.getConfigLong(threadName + "_idlesleep", idlesleep);
            busysleep = sb.getConfigLong(threadName + "_busysleep", busysleep);
        }
        if (setProfile) {
            // load with new values
            idlesleep = Long
                    .parseLong(d(defaultSettings.get(threadName + "_idlesleep"), String.valueOf(idlesleep)));
            busysleep = Long
                    .parseLong(d(defaultSettings.get(threadName + "_busysleep"), String.valueOf(busysleep)));
            memprereq = Long
                    .parseLong(d(defaultSettings.get(threadName + "_memprereq"), String.valueOf(memprereq)));
            loadprereq = Double.parseDouble(
                    d(defaultSettings.get(threadName + "_loadprereq"), String.valueOf(loadprereq)));
            // check values to prevent short-cut loops
            if (idlesleep < 1000)
                idlesleep = 1000;
            //if (threadName.equals(plasmaSwitchboardConstants.CRAWLJOB_LOCAL_CRAWL) && (busysleep < 50)) busysleep = 50;
            sb.setThreadPerformance(threadName, idlesleep, busysleep, memprereq, loadprereq);
        }
        prop.put("table_" + c + "_idlesleep", idlesleep);
        prop.put("table_" + c + "_busysleep", busysleep);
        prop.put("table_" + c + "_memprereq", memprereq / 1024);
        prop.put("table_" + c + "_loadprereq", loadprereq);
        // disallow setting of memprereq for indexer to prevent db from throwing OOMs
        // prop.put("table_" + c + "_disabled", /*(threadName.endsWith("_indexing")) ? 1 :*/ "0");
        prop.put("table_" + c + "_recommendation", threadName.endsWith("_indexing") ? "1" : "0");
        prop.putNum("table_" + c + "_recommendation_value",
                rwi == null ? 0 : threadName.endsWith("_indexing") ? (rwi.minMem() / 1024) : 0);
        c++;
    }
    prop.put("table", c);

    c = 0;
    final int[] speedValues = { 200, 150, 100, 50, 25, 10 };
    final int usedspeed = sb.getConfigInt("performanceSpeed", 100);
    for (final int speed : speedValues) {
        prop.put("speed_" + c + "_value", speed);
        prop.put("speed_" + c + "_label", speed + " %");
        prop.put("speed_" + c + "_used", (speed == usedspeed) ? "1" : "0");
        c++;
    }
    prop.put("speed", c);

    if ((post != null) && (post.containsKey("cacheSizeSubmit"))) {
        final int wordCacheMaxCount = post.getInt("wordCacheMaxCount", 20000);
        sb.setConfig(SwitchboardConstants.WORDCACHE_MAX_COUNT, Integer.toString(wordCacheMaxCount));
        if (rwi != null)
            rwi.setBufferMaxWordCount(wordCacheMaxCount);
    }

    /* Setting remote searches max loads */
    if (post != null) {
        if (post.containsKey("setRemoteSearchLoads")) {
            float loadValue = post.getFloat("remoteSearchRWIMaxLoad",
                    sb.getConfigFloat(SwitchboardConstants.REMOTESEARCH_MAXLOAD_RWI,
                            SwitchboardConstants.REMOTESEARCH_MAXLOAD_RWI_DEFAULT));
            sb.setConfig(SwitchboardConstants.REMOTESEARCH_MAXLOAD_RWI, loadValue);

            loadValue = post.getFloat("remoteSearchSolrMaxLoad",
                    sb.getConfigFloat(SwitchboardConstants.REMOTESEARCH_MAXLOAD_SOLR,
                            SwitchboardConstants.REMOTESEARCH_MAXLOAD_SOLR_DEFAULT));
            sb.setConfig(SwitchboardConstants.REMOTESEARCH_MAXLOAD_SOLR, loadValue);
        } else if (post.containsKey("defaultRemoteSearchLoads")) {
            sb.setConfig(SwitchboardConstants.REMOTESEARCH_MAXLOAD_RWI,
                    SwitchboardConstants.REMOTESEARCH_MAXLOAD_RWI_DEFAULT);
            sb.setConfig(SwitchboardConstants.REMOTESEARCH_MAXLOAD_SOLR,
                    SwitchboardConstants.REMOTESEARCH_MAXLOAD_SOLR_DEFAULT);
        }
    }

    if ((post != null) && (post.containsKey("poolConfig"))) {

        /*
         * configuring the crawler pool
         */
        // get the current crawler pool configuration
        int maxBusy = post.getInt("Crawler Pool_maxActive", 8);

        // storing the new values into configfile
        sb.setConfig(SwitchboardConstants.CRAWLER_THREADS_ACTIVE_MAX, maxBusy);

        /*
         * configuring the robots.txt loading pool
         */
        // get the current crawler pool configuration
        maxBusy = post.getInt("Robots.txt Pool_maxActive",
                SwitchboardConstants.ROBOTS_TXT_THREADS_ACTIVE_MAX_DEFAULT);

        // storing the new values into configfile
        sb.setConfig(SwitchboardConstants.ROBOTS_TXT_THREADS_ACTIVE_MAX, maxBusy);

        /*
         * configuring the http pool
         */
        try {
            maxBusy = post.getInt("httpd Session Pool_maxActive", 8);
        } catch (final NumberFormatException e) {
            maxBusy = 8;
        }

        ConnectionInfo.setServerMaxcount(maxBusy);

        // storing the new values into configfile
        sb.setConfig("httpdMaxBusySessions", maxBusy);

    }

    if ((post != null) && (post.containsKey("connectionPoolConfig"))) {

        /* Configure the general outgoing HTTP connection pool */
        int maxTotal = post.getInt(SwitchboardConstants.HTTP_OUTGOING_POOL_GENERAL_MAX_TOTAL,
                SwitchboardConstants.HTTP_OUTGOING_POOL_GENERAL_MAX_TOTAL_DEFAULT);
        if (maxTotal > 0) {
            sb.setConfig(SwitchboardConstants.HTTP_OUTGOING_POOL_GENERAL_MAX_TOTAL, maxTotal);
            HTTPClient.initPoolMaxConnections(HTTPClient.CONNECTION_MANAGER, maxTotal);
        }

        /* Configure the remote Solr outgoing HTTP connection pool */
        maxTotal = post.getInt(SwitchboardConstants.HTTP_OUTGOING_POOL_REMOTE_SOLR_MAX_TOTAL,
                SwitchboardConstants.HTTP_OUTGOING_POOL_REMOTE_SOLR_MAX_TOTAL_DEFAULT);
        if (maxTotal > 0) {
            sb.setConfig(SwitchboardConstants.HTTP_OUTGOING_POOL_REMOTE_SOLR_MAX_TOTAL, maxTotal);
            RemoteInstance.initPoolMaxConnections(RemoteInstance.CONNECTION_MANAGER, maxTotal);
        }
    }

    if ((post != null) && (post.containsKey("onlineCautionSubmit"))) {
        sb.setConfig(SwitchboardConstants.PROXY_ONLINE_CAUTION_DELAY,
                Integer.toString(post.getInt("crawlPauseProxy", 30000)));
        sb.setConfig(SwitchboardConstants.LOCALSEACH_ONLINE_CAUTION_DELAY,
                Integer.toString(post.getInt("crawlPauseLocalsearch", 30000)));
        sb.setConfig(SwitchboardConstants.REMOTESEARCH_ONLINE_CAUTION_DELAY,
                Integer.toString(post.getInt("crawlPauseRemotesearch", 30000)));
    }

    // table cache settings
    prop.putNum("wordCacheSize", indexSegment.RWIBufferCount());
    prop.putNum("wordCacheSizeKBytes", rwi == null ? 0 : rwi.getBufferSizeBytes() / 1024L);
    prop.putNum("maxURLinCache", rwi == null ? 0 : rwi.getBufferMaxReferences());
    prop.putNum("maxAgeOfCache", rwi == null ? 0 : rwi.getBufferMaxAge() / 1000 / 60); // minutes
    prop.putNum("minAgeOfCache", rwi == null ? 0 : rwi.getBufferMinAge() / 1000 / 60); // minutes
    prop.putNum("maxWaitingWordFlush", sb.getConfigLong("maxWaitingWordFlush", 180));
    prop.put("wordCacheMaxCount", sb.getConfigLong(SwitchboardConstants.WORDCACHE_MAX_COUNT, 20000));
    prop.put("crawlPauseProxy", sb.getConfigLong(SwitchboardConstants.PROXY_ONLINE_CAUTION_DELAY, 30000));
    prop.put("crawlPauseLocalsearch",
            sb.getConfigLong(SwitchboardConstants.LOCALSEACH_ONLINE_CAUTION_DELAY, 30000));
    prop.put("crawlPauseRemotesearch",
            sb.getConfigLong(SwitchboardConstants.REMOTESEARCH_ONLINE_CAUTION_DELAY, 30000));
    prop.putNum("crawlPauseProxyCurrent", (System.currentTimeMillis() - sb.proxyLastAccess) / 1000);
    prop.putNum("crawlPauseLocalsearchCurrent", (System.currentTimeMillis() - sb.localSearchLastAccess) / 1000);
    prop.putNum("crawlPauseRemotesearchCurrent",
            (System.currentTimeMillis() - sb.remoteSearchLastAccess) / 1000);

    // table thread pool settings
    prop.put("pool_0_name", "Crawler Pool");
    prop.put("pool_0_maxActive", sb.getConfigLong(SwitchboardConstants.CRAWLER_THREADS_ACTIVE_MAX, 0));
    prop.put("pool_0_numActive", sb.crawlQueues.activeWorkerEntries().size());

    prop.put("pool_1_name", "Robots.txt Pool");
    prop.put("pool_1_maxActive", sb.getConfigInt(SwitchboardConstants.ROBOTS_TXT_THREADS_ACTIVE_MAX,
            SwitchboardConstants.ROBOTS_TXT_THREADS_ACTIVE_MAX_DEFAULT));
    prop.put("pool_1_numActive", sb.crawlQueues.activeWorkerEntries().size());

    prop.put("pool_2_name", "httpd Session Pool");
    prop.put("pool_2_maxActive", ConnectionInfo.getServerMaxcount());
    prop.put("pool_2_numActive", ConnectionInfo.getServerCount());

    prop.put("pool", "3");

    /* Connection pools settings */
    prop.put(SwitchboardConstants.HTTP_OUTGOING_POOL_GENERAL_MAX_TOTAL,
            sb.getConfigInt(SwitchboardConstants.HTTP_OUTGOING_POOL_GENERAL_MAX_TOTAL,
                    SwitchboardConstants.HTTP_OUTGOING_POOL_GENERAL_MAX_TOTAL_DEFAULT));
    prop.put(SwitchboardConstants.HTTP_OUTGOING_POOL_REMOTE_SOLR_MAX_TOTAL,
            sb.getConfigInt(SwitchboardConstants.HTTP_OUTGOING_POOL_REMOTE_SOLR_MAX_TOTAL,
                    SwitchboardConstants.HTTP_OUTGOING_POOL_REMOTE_SOLR_MAX_TOTAL_DEFAULT));
    /* Connection pools stats */
    PoolStats stats = HTTPClient.CONNECTION_MANAGER.getTotalStats();
    prop.put("pool.general.leased", stats.getLeased());
    prop.put("pool.general.available", stats.getAvailable());
    prop.put("pool.general.pending", stats.getPending());

    stats = RemoteInstance.CONNECTION_MANAGER.getTotalStats();
    prop.put("pool.remoteSolr.leased", stats.getLeased());
    prop.put("pool.remoteSolr.available", stats.getAvailable());
    prop.put("pool.remoteSolr.pending", stats.getPending());

    /* Remote searches max loads settings */
    prop.put("remoteSearchRWIMaxLoad", sb.getConfigFloat(SwitchboardConstants.REMOTESEARCH_MAXLOAD_RWI,
            SwitchboardConstants.REMOTESEARCH_MAXLOAD_RWI_DEFAULT));
    prop.put("remoteSearchSolrMaxLoad", sb.getConfigFloat(SwitchboardConstants.REMOTESEARCH_MAXLOAD_SOLR,
            SwitchboardConstants.REMOTESEARCH_MAXLOAD_SOLR_DEFAULT));

    // parse initialization memory settings
    final String Xmx = sb.getConfig("javastart_Xmx", "Xmx600m").substring(3);
    prop.put("Xmx", Xmx.substring(0, Xmx.length() - 1));
    final String Xms = sb.getConfig("javastart_Xms", "Xms600m").substring(3);
    prop.put("Xms", Xms.substring(0, Xms.length() - 1));

    final long diskFree = sb.getConfigLong(SwitchboardConstants.RESOURCE_DISK_FREE_MIN_STEADYSTATE, 3000L);
    final long diskFreeHardlimit = sb.getConfigLong(SwitchboardConstants.RESOURCE_DISK_FREE_MIN_UNDERSHOT,
            1000L);
    final long memoryAcceptDHT = sb.getConfigLong(SwitchboardConstants.MEMORY_ACCEPTDHT, 50L);
    final boolean observerTrigger = !MemoryControl.properState();
    prop.put("diskFree", diskFree);
    prop.put("diskFreeHardlimit", diskFreeHardlimit);
    prop.put("diskFreeAutoregulate", sb.getConfigBool(SwitchboardConstants.RESOURCE_DISK_FREE_AUTOREGULATE,
            SwitchboardConstants.RESOURCE_DISK_FREE_AUTOREGULATE_DEFAULT) ? 1 : 0);
    prop.put("diskUsed", sb.getConfigLong(SwitchboardConstants.RESOURCE_DISK_USED_MAX_STEADYSTATE,
            SwitchboardConstants.RESOURCE_DISK_USED_MAX_STEADYSTATE_DEFAULT));
    prop.put("diskUsedHardlimit", sb.getConfigLong(SwitchboardConstants.RESOURCE_DISK_USED_MAX_OVERSHOT,
            SwitchboardConstants.RESOURCE_DISK_USED_MAX_OVERSHOT_DEFAULT));
    prop.put("diskUsedAutoregulate", sb.getConfigBool(SwitchboardConstants.RESOURCE_DISK_USED_AUTOREGULATE,
            SwitchboardConstants.RESOURCE_DISK_USED_AUTOREGULATE_DEFAULT) ? 1 : 0);
    prop.put("memoryAcceptDHT", memoryAcceptDHT);
    if (observerTrigger)
        prop.put("observerTrigger", "1");

    // return rewrite values for templates
    return prop;
}

From source file:SftpClientFactory.java

/**
 * Creates a new connection to the server.
 * @param hostname The name of the host to connect to.
 * @param port The port to use.// w w  w .ja  v  a  2  s . co  m
 * @param username The user's id.
 * @param password The user's password.
 * @param fileSystemOptions The FileSystem options.
 * @return A Session.
 * @throws FileSystemException if an error occurs.
 */
public static Session createConnection(String hostname, int port, char[] username, char[] password,
        FileSystemOptions fileSystemOptions) throws FileSystemException {
    JSch jsch = new JSch();

    File sshDir = null;

    // new style - user passed
    File knownHostsFile = SftpFileSystemConfigBuilder.getInstance().getKnownHosts(fileSystemOptions);
    File[] identities = SftpFileSystemConfigBuilder.getInstance().getIdentities(fileSystemOptions);

    if (knownHostsFile != null) {
        try {
            jsch.setKnownHosts(knownHostsFile.getAbsolutePath());
        } catch (JSchException e) {
            throw new FileSystemException("vfs.provider.sftp/known-hosts.error",
                    knownHostsFile.getAbsolutePath(), e);
        }
    } else {
        sshDir = findSshDir();
        // Load the known hosts file
        knownHostsFile = new File(sshDir, "known_hosts");
        if (knownHostsFile.isFile() && knownHostsFile.canRead()) {
            try {
                jsch.setKnownHosts(knownHostsFile.getAbsolutePath());
            } catch (JSchException e) {
                throw new FileSystemException("vfs.provider.sftp/known-hosts.error",
                        knownHostsFile.getAbsolutePath(), e);
            }
        }
    }

    if (identities != null) {
        for (int iterIdentities = 0; iterIdentities < identities.length; iterIdentities++) {
            final File privateKeyFile = identities[iterIdentities];
            try {
                jsch.addIdentity(privateKeyFile.getAbsolutePath());
            } catch (final JSchException e) {
                throw new FileSystemException("vfs.provider.sftp/load-private-key.error", privateKeyFile, e);
            }
        }
    } else {
        if (sshDir == null) {
            sshDir = findSshDir();
        }

        // Load the private key (rsa-key only)
        final File privateKeyFile = new File(sshDir, "id_rsa");
        if (privateKeyFile.isFile() && privateKeyFile.canRead()) {
            try {
                jsch.addIdentity(privateKeyFile.getAbsolutePath());
            } catch (final JSchException e) {
                throw new FileSystemException("vfs.provider.sftp/load-private-key.error", privateKeyFile, e);
            }
        }
    }

    Session session;
    try {
        session = jsch.getSession(new String(username), hostname, port);
        if (password != null) {
            session.setPassword(new String(password));
        }

        Integer timeout = SftpFileSystemConfigBuilder.getInstance().getTimeout(fileSystemOptions);
        if (timeout != null) {
            session.setTimeout(timeout.intValue());
        }

        UserInfo userInfo = SftpFileSystemConfigBuilder.getInstance().getUserInfo(fileSystemOptions);
        if (userInfo != null) {
            session.setUserInfo(userInfo);
        }

        Properties config = new Properties();

        //set StrictHostKeyChecking property
        String strictHostKeyChecking = SftpFileSystemConfigBuilder.getInstance()
                .getStrictHostKeyChecking(fileSystemOptions);
        if (strictHostKeyChecking != null) {
            config.setProperty("StrictHostKeyChecking", strictHostKeyChecking);
        }
        //set PreferredAuthentications property
        String preferredAuthentications = SftpFileSystemConfigBuilder.getInstance()
                .getPreferredAuthentications(fileSystemOptions);
        if (preferredAuthentications != null) {
            config.setProperty("PreferredAuthentications", preferredAuthentications);
        }

        //set compression property
        String compression = SftpFileSystemConfigBuilder.getInstance().getCompression(fileSystemOptions);
        if (compression != null) {
            config.setProperty("compression.s2c", compression);
            config.setProperty("compression.c2s", compression);
        }

        String proxyHost = SftpFileSystemConfigBuilder.getInstance().getProxyHost(fileSystemOptions);
        if (proxyHost != null) {
            int proxyPort = SftpFileSystemConfigBuilder.getInstance().getProxyPort(fileSystemOptions);
            SftpFileSystemConfigBuilder.ProxyType proxyType = SftpFileSystemConfigBuilder.getInstance()
                    .getProxyType(fileSystemOptions);
            Proxy proxy = null;
            if (SftpFileSystemConfigBuilder.PROXY_HTTP.equals(proxyType)) {
                if (proxyPort != 0) {
                    proxy = new ProxyHTTP(proxyHost, proxyPort);
                } else {
                    proxy = new ProxyHTTP(proxyHost);
                }
            } else if (SftpFileSystemConfigBuilder.PROXY_SOCKS5.equals(proxyType)) {
                if (proxyPort != 0) {
                    proxy = new ProxySOCKS5(proxyHost, proxyPort);
                } else {
                    proxy = new ProxySOCKS5(proxyHost);
                }
            }

            if (proxy != null) {
                session.setProxy(proxy);
            }
        }

        //set properties for the session
        if (config.size() > 0) {
            session.setConfig(config);
        }
        session.setDaemonThread(true);
        session.connect();
    } catch (final Exception exc) {
        throw new FileSystemException("vfs.provider.sftp/connect.error", new Object[] { hostname }, exc);
    }

    return session;
}

From source file:com.piaoyou.util.FileUtil.java

public static void copyFile(File org, File des, boolean overwrite) throws IOException {

    if (!org.exists()) {
        throw new FileNotFoundException("Cannot find the source file: " + org.getAbsolutePath());
    }/*from   w w  w.  j  av  a2  s .co  m*/
    if (!org.canRead()) {
        throw new IOException("Cannot read the source file: " + org.getAbsolutePath());
    }

    if (overwrite == false) {
        if (des.exists()) {
            return;
        }
    } else {
        if (des.exists()) {
            if (!des.canWrite()) {
                throw new IOException("Cannot write the destination file: " + des.getAbsolutePath());
            }
        } else {
            if (!des.createNewFile()) {
                throw new IOException("Cannot write the destination file: " + des.getAbsolutePath());
            }
        }
    }

    BufferedInputStream inputStream = null;
    BufferedOutputStream outputStream = null;
    byte[] block = new byte[1024];
    try {
        inputStream = new BufferedInputStream(new FileInputStream(org));
        outputStream = new BufferedOutputStream(new FileOutputStream(des));
        while (true) {
            int readLength = inputStream.read(block);
            if (readLength == -1)
                break;// end of file
            outputStream.write(block, 0, readLength);
        }
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException ex) {
                log.error("Cannot close stream", ex);
            }
        }
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException ex) {
                log.error("Cannot close stream", ex);
            }
        }
    }
}

From source file:com.ikon.util.impexp.RepositoryImporter.java

/**
 * Import documents from filesystem into document repository (recursive).
 *//*from  w  w  w .  j a v  a  2  s .  c  o m*/
private static ImpExpStats importDocumentsHelper(String token, File fs, String fldPath, String metadata,
        boolean history, boolean uuid, Writer out, InfoDecorator deco)
        throws FileNotFoundException, PathNotFoundException, AccessDeniedException, RepositoryException,
        IOException, DatabaseException, ExtensionException, AutomationException {
    log.debug("importDocumentsHelper({}, {}, {}, {}, {}, {}, {}, {})",
            new Object[] { token, fs, fldPath, metadata, history, uuid, out, deco });
    File[] files = fs.listFiles(new RepositoryImporter.NoVersionFilenameFilter());
    ImpExpStats stats = new ImpExpStats();
    FolderModule fm = ModuleManager.getFolderModule();
    MetadataAdapter ma = MetadataAdapter.getInstance(token);
    ma.setRestoreUuid(uuid);
    Gson gson = new Gson();

    for (int i = 0; i < files.length; i++) {
        String fileName = files[i].getName();

        if (!fileName.endsWith(Config.EXPORT_METADATA_EXT) && !fileName.endsWith(".xml")) {
            if (files[i].isDirectory()) {
                Folder fld = new Folder();
                boolean api = false;
                int importedFolder = 0;
                log.info("Directory: {}", files[i]);

                try {
                    if (metadata.equals("JSON")) {
                        // Read serialized folder metadata
                        File jsFile = new File(files[i].getPath() + Config.EXPORT_METADATA_EXT);
                        log.info("Folder Metadata: {}", jsFile.getPath());

                        if (jsFile.exists() && jsFile.canRead()) {
                            FileReader fr = new FileReader(jsFile);
                            FolderMetadata fmd = gson.fromJson(fr, FolderMetadata.class);
                            fr.close();

                            // Apply metadata
                            fld.setPath(fldPath + "/" + fileName);
                            fmd.setPath(fld.getPath());
                            ma.importWithMetadata(fmd);

                            if (out != null) {
                                out.write(deco.print(files[i].getPath(), files[i].length(), null));
                                out.flush();
                            }
                        } else {
                            log.warn("Unable to read metadata file: {}", jsFile.getPath());
                            api = true;
                        }
                    } else if (metadata.equals("XML")) {
                        // Read serialized folder metadata
                        File jsFile = new File(files[i].getPath() + ".xml");
                        log.info("Folder Metadata: {}", jsFile.getPath());

                        if (jsFile.exists() && jsFile.canRead()) {
                            FileReader fr = new FileReader(jsFile);
                            JAXBContext jaxbContext = JAXBContext.newInstance(FolderMetadata.class);

                            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
                            FolderMetadata fmd = (FolderMetadata) jaxbUnmarshaller.unmarshal(fr);
                            fr.close();

                            // Apply metadata
                            fld.setPath(fldPath + "/" + fileName);
                            fmd.setPath(fld.getPath());
                            ma.importWithMetadata(fmd);

                            if (out != null) {
                                out.write(deco.print(files[i].getPath(), files[i].length(), null));
                                out.flush();
                            }
                        } else {
                            log.warn("Unable to read metadata file: {}", jsFile.getPath());
                            api = true;
                        }
                    } else {
                        api = true;
                    }

                    if (api) {
                        fld.setPath(fldPath + "/" + fileName);
                        fm.create(token, fld);
                        FileLogger.info(BASE_NAME, "Created folder ''{0}''", fld.getPath());

                        if (out != null) {
                            out.write(deco.print(files[i].getPath(), files[i].length(), null));
                            out.flush();
                        }
                    }

                    importedFolder = 1;
                } catch (ItemExistsException e) {
                    log.warn("ItemExistsException: {}", e.getMessage());

                    if (out != null) {
                        out.write(deco.print(files[i].getPath(), files[i].length(), "ItemExists"));
                        out.flush();
                    }

                    stats.setOk(false);
                    FileLogger.error(BASE_NAME, "ItemExistsException ''{0}''", fld.getPath());
                } catch (JsonParseException e) {
                    log.warn("JsonParseException: {}", e.getMessage());

                    if (out != null) {
                        out.write(deco.print(files[i].getPath(), files[i].length(), "Json"));
                        out.flush();
                    }

                    stats.setOk(false);
                    FileLogger.error(BASE_NAME, "JsonParseException ''{0}''", fld.getPath());
                } catch (JAXBException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                ImpExpStats tmp = importDocumentsHelper(token, files[i], fld.getPath(), metadata, history, uuid,
                        out, deco);

                // Stats
                stats.setOk(stats.isOk() && tmp.isOk());
                stats.setSize(stats.getSize() + tmp.getSize());
                stats.setMails(stats.getMails() + tmp.getMails());
                stats.setDocuments(stats.getDocuments() + tmp.getDocuments());
                stats.setFolders(stats.getFolders() + tmp.getFolders() + importedFolder);
            } else {
                log.info("File: {}", files[i]);

                if (fileName.endsWith(".eml")) {
                    log.info("Mail: {}", files[i]);
                    ImpExpStats tmp = importMail(token, fs, fldPath, fileName, files[i], metadata, out, deco);

                    // Stats
                    stats.setOk(stats.isOk() && tmp.isOk());
                    stats.setSize(stats.getSize() + tmp.getSize());
                    stats.setMails(stats.getMails() + tmp.getMails());
                } else {
                    log.info("Document: {}", files[i]);
                    ImpExpStats tmp = importDocument(token, fs, fldPath, fileName, files[i], metadata, history,
                            out, deco);

                    // Stats
                    stats.setOk(stats.isOk() && tmp.isOk());
                    stats.setSize(stats.getSize() + tmp.getSize());
                    stats.setDocuments(stats.getDocuments() + tmp.getDocuments());
                }
            }
        }
    }

    log.debug("importDocumentsHelper: {}", stats);
    return stats;
}

From source file:gmgen.util.MiscUtilities.java

/**
 * Copy a file/*from   w w  w  . j  a v  a 2 s  .c o m*/
 * @param from_file
 * @param to_file
 * @throws IOException
 */
public static void copy(File from_file, File to_file) throws IOException {
    // First make sure the source file exists, is a file, and is readable.
    if (!from_file.exists()) {
        throw new IOException("FileCopy: no such source file: " + from_file.getPath());
    }

    if (!from_file.isFile()) {
        throw new IOException("FileCopy: can't copy directory: " + from_file.getPath());
    }

    if (!from_file.canRead()) {
        throw new IOException("FileCopy: source file is unreadable: " + from_file.getPath());
    }

    // If the destination is a directory, use the source file name
    // as the destination file name
    if (to_file.isDirectory()) {
        to_file = new File(to_file, from_file.getName());
    }

    // If the destination exists, make sure it is a writeable file
    // and ask before overwriting it.  If the destination doesn't
    // exist, make sure the directory exists and is writeable.
    if (to_file.exists()) {
        if (!to_file.canWrite()) {
            throw new IOException("FileCopy: destination file is unwriteable: " + to_file.getPath());
        }

        // Ask whether to overwrite it
        int choice = JOptionPane.showConfirmDialog(null, "Overwrite existing file " + to_file.getPath(),
                "File Exists", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

        if (choice != JOptionPane.YES_OPTION) {
            throw new IOException("FileCopy: existing file was not overwritten.");
        }
    } else {
        // if file doesn't exist, check if directory exists and is writeable.
        // If getParent() returns null, then the directory is the current dir.
        // so look up the user.dir system property to find out what that is.
        String parent = to_file.getParent(); // Get the destination directory

        if (parent == null) {
            parent = Globals.getDefaultPath(); // or CWD
        }

        File dir = new File(parent); // Convert it to a file.

        if (!dir.exists()) {
            throw new IOException("FileCopy: destination directory doesn't exist: " + parent);
        }

        if (dir.isFile()) {
            throw new IOException("FileCopy: destination is not a directory: " + parent);
        }

        if (!dir.canWrite()) {
            throw new IOException("FileCopy: destination directory is unwriteable: " + parent);
        }
    }

    // If we've gotten this far, then everything is okay.
    // So we copy the file, a buffer of bytes at a time.
    FileInputStream from = null; // Stream to read from source
    FileOutputStream to = null; // Stream to write to destination

    try {
        from = new FileInputStream(from_file); // Create input stream
        to = new FileOutputStream(to_file); // Create output stream

        byte[] buffer = new byte[4096]; // A buffer to hold file contents
        int bytes_read; // How many bytes in buffer

        while ((bytes_read = from.read(buffer)) != -1) { // Read bytes until EOF
            to.write(buffer, 0, bytes_read); //   write bytes
        }
    }

    // Always close the streams, even if exceptions were thrown
    finally {
        if (from != null) {
            try {
                from.close();
            } catch (IOException e) {
                //TODO: Should this really be ignored?
            }
        }

        if (to != null) {
            try {
                to.close();
            } catch (IOException e) {
                //TODO: Should this really be ignored?
            }
        }
    }
}

From source file:com.liferay.util.FileUtil.java

/**
 * Directory is valid if it exists, does not represent a file, and can be read.
 *///from w ww . j  a  va2  s .  c o  m
static private void validateDirectory(File aDirectory) throws FileNotFoundException {
    if (aDirectory == null) {
        throw new IllegalArgumentException("Directory should not be null.");
    }
    if (!aDirectory.exists()) {
        throw new FileNotFoundException("Directory does not exist: " + aDirectory);
    }
    if (!aDirectory.isDirectory()) {
        throw new IllegalArgumentException("Is not a directory: " + aDirectory);
    }
    if (!aDirectory.canRead()) {
        throw new IllegalArgumentException("Directory cannot be read: " + aDirectory);
    }
}

From source file:File.TXT.ReadFile.java

/**
 * @param args the command line arguments
 *///from   w  ww .j av  a2  s.c  o  m
public String read(String path, String nama_file) {

    String hasil = "";
    File f = new File(path + nama_file);
    if (f.canRead()) {
        try {
            hasil = FileUtils.readFileToString(f);
            return hasil;
        } catch (IOException ex) {
            hasil = ex.getMessage();
            Logger.getLogger(ReadFile.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    return hasil;
}

From source file:ca.uhn.hl7v2.testpanel.controller.Prefs.java

public static Prefs getInstance() {
    try {/*from w ww .jav a  2  s .c om*/
        ourJaxbContext = JAXBContext.newInstance(Prefs.class, XmlFormat.class);
    } catch (JAXBException e) {
        throw new Error(e);
    }

    if (ourInstance == null) {
        File prefsFile = getPrefsFile();
        if (prefsFile.exists() && prefsFile.isFile() && prefsFile.canRead()) {
            try {
                ourInstance = (Prefs) ourJaxbContext.createUnmarshaller().unmarshal(prefsFile);
            } catch (Exception e) {
                ourLog.error("Failed to load prefs- Using default!", e);
                ourInstance = new Prefs();
            }
        } else {
            ourInstance = new Prefs();
            try {
                if (ourPrefs.keys().length > 0) {
                    ourInstance.initFromJavaPrefs(); // hopefully we will do
                    // away
                    // with this
                    // eventually
                }
            } catch (BackingStoreException e) {
                // we can ignore this
            }
        }
    }
    return ourInstance;
}

From source file:com.openkm.util.impexp.RepositoryImporter.java

/**
 * Import document./*from w  w w .  j ava  2  s  .  co m*/
 */
private static ImpExpStats importDocument(String token, File fs, String fldPath, String fileName, File fDoc,
        boolean metadata, boolean history, Writer out, InfoDecorator deco)
        throws IOException, RepositoryException, DatabaseException, PathNotFoundException,
        AccessDeniedException, ExtensionException, AutomationException {
    FileInputStream fisContent = new FileInputStream(fDoc);
    MetadataAdapter ma = MetadataAdapter.getInstance(token);
    DocumentModule dm = ModuleManager.getDocumentModule();
    ImpExpStats stats = new ImpExpStats();
    int size = fisContent.available();
    Document doc = new Document();
    Gson gson = new Gson();
    boolean api = false;

    try {
        // Metadata
        if (metadata) {
            // Read serialized document metadata
            File jsFile = new File(fDoc.getPath() + Config.EXPORT_METADATA_EXT);
            log.info("Document Metadata File: {}", jsFile.getPath());

            if (jsFile.exists() && jsFile.canRead()) {
                FileReader fr = new FileReader(jsFile);
                DocumentMetadata dmd = gson.fromJson(fr, DocumentMetadata.class);
                doc.setPath(fldPath + "/" + fileName);
                dmd.setPath(doc.getPath());
                IOUtils.closeQuietly(fr);
                log.info("Document Metadata: {}", dmd);

                if (history) {
                    File[] vhFiles = fs.listFiles(new RepositoryImporter.VersionFilenameFilter(fileName));
                    List<File> listFiles = Arrays.asList(vhFiles);
                    Collections.sort(listFiles, FilenameVersionComparator.getInstance());
                    boolean first = true;

                    for (File vhf : vhFiles) {
                        String vhfName = vhf.getName();
                        int idx = vhfName.lastIndexOf('#', vhfName.length() - 2);
                        String verName = vhfName.substring(idx + 2, vhfName.length() - 1);
                        FileInputStream fis = new FileInputStream(vhf);
                        File jsVerFile = new File(vhf.getPath() + Config.EXPORT_METADATA_EXT);
                        log.info("Document Version Metadata File: {}", jsVerFile.getPath());

                        if (jsVerFile.exists() && jsVerFile.canRead()) {
                            FileReader verFr = new FileReader(jsVerFile);
                            VersionMetadata vmd = gson.fromJson(verFr, VersionMetadata.class);
                            IOUtils.closeQuietly(verFr);

                            if (first) {
                                dmd.setVersion(vmd);
                                size = fis.available();
                                ma.importWithMetadata(dmd, fis);
                                first = false;
                            } else {
                                log.info("Document Version Metadata: {}", vmd);
                                size = fis.available();
                                ma.importWithMetadata(doc.getPath(), vmd, fis);
                            }
                        } else {
                            log.warn("Unable to read metadata file: {}", jsVerFile.getPath());
                        }

                        IOUtils.closeQuietly(fis);
                        FileLogger.info(BASE_NAME, "Created document ''{0}'' version ''{1}''", doc.getPath(),
                                verName);
                        log.info("Created document '{}' version '{}'", doc.getPath(), verName);
                    }
                } else {
                    // Apply metadata
                    ma.importWithMetadata(dmd, fisContent);
                    FileLogger.info(BASE_NAME, "Created document ''{0}''", doc.getPath());
                    log.info("Created document '{}'", doc.getPath());
                }
            } else {
                log.warn("Unable to read metadata file: {}", jsFile.getPath());
                api = true;
            }
        } else {
            api = true;
        }

        if (api) {
            doc.setPath(fldPath + "/" + fileName);

            // Version history
            if (history) {
                File[] vhFiles = fs.listFiles(new RepositoryImporter.VersionFilenameFilter(fileName));
                List<File> listFiles = Arrays.asList(vhFiles);
                Collections.sort(listFiles, FilenameVersionComparator.getInstance());
                boolean first = true;

                for (File vhf : vhFiles) {
                    String vhfName = vhf.getName();
                    int idx = vhfName.lastIndexOf('#', vhfName.length() - 2);
                    String verName = vhfName.substring(idx + 2, vhfName.length() - 1);
                    FileInputStream fis = new FileInputStream(vhf);

                    if (first) {
                        dm.create(token, doc, fis);
                        first = false;
                    } else {
                        dm.checkout(token, doc.getPath());
                        dm.checkin(token, doc.getPath(), fis, "Imported from administration");
                    }

                    IOUtils.closeQuietly(fis);
                    FileLogger.info(BASE_NAME, "Created document ''{0}'' version ''{1}''", doc.getPath(),
                            verName);
                    log.info("Created document '{}' version '{}'", doc.getPath(), verName);
                }
            } else {
                dm.create(token, doc, fisContent);
                FileLogger.info(BASE_NAME, "Created document ''{0}''", doc.getPath());
                log.info("Created document ''{}''", doc.getPath());
            }
        }

        if (out != null) {
            out.write(deco.print(fDoc.getPath(), fDoc.length(), null));
            out.flush();
        }

        // Stats
        stats.setSize(stats.getSize() + size);
        stats.setDocuments(stats.getDocuments() + 1);
    } catch (UnsupportedMimeTypeException e) {
        log.warn("UnsupportedMimeTypeException: {}", e.getMessage());

        if (out != null) {
            out.write(deco.print(fDoc.getPath(), fDoc.length(), "UnsupportedMimeType"));
            out.flush();
        }

        stats.setOk(false);
        FileLogger.error(BASE_NAME, "UnsupportedMimeTypeException ''{0}''", doc.getPath());
    } catch (FileSizeExceededException e) {
        log.warn("FileSizeExceededException: {}", e.getMessage());

        if (out != null) {
            out.write(deco.print(fDoc.getPath(), fDoc.length(), "FileSizeExceeded"));
            out.flush();
        }

        stats.setOk(false);
        FileLogger.error(BASE_NAME, "FileSizeExceededException ''{0}''", doc.getPath());
    } catch (UserQuotaExceededException e) {
        log.warn("UserQuotaExceededException: {}", e.getMessage());

        if (out != null) {
            out.write(deco.print(fDoc.getPath(), fDoc.length(), "UserQuotaExceeded"));
            out.flush();
        }

        stats.setOk(false);
        FileLogger.error(BASE_NAME, "UserQuotaExceededException ''{0}''", doc.getPath());
    } catch (VirusDetectedException e) {
        log.warn("VirusWarningException: {}", e.getMessage());

        if (out != null) {
            out.write(deco.print(fDoc.getPath(), fDoc.length(), "VirusWarningException"));
            out.flush();
        }

        stats.setOk(false);
        FileLogger.error(BASE_NAME, "VirusWarningException ''{0}''", doc.getPath());
    } catch (ItemExistsException e) {
        log.warn("ItemExistsException: {}", e.getMessage());

        if (out != null) {
            out.write(deco.print(fDoc.getPath(), fDoc.length(), "ItemExists"));
            out.flush();
        }

        stats.setOk(false);
        FileLogger.error(BASE_NAME, "ItemExistsException ''{0}''", doc.getPath());
    } catch (LockException e) {
        log.warn("LockException: {}", e.getMessage());

        if (out != null) {
            out.write(deco.print(fDoc.getPath(), fDoc.length(), "Lock"));
            out.flush();
        }

        stats.setOk(false);
        FileLogger.error(BASE_NAME, "LockException ''{0}''", doc.getPath());
    } catch (VersionException e) {
        log.warn("VersionException: {}", e.getMessage());

        if (out != null) {
            out.write(deco.print(fDoc.getPath(), fDoc.length(), "Version"));
            out.flush();
        }

        stats.setOk(false);
        FileLogger.error(BASE_NAME, "VersionException ''{0}''", doc.getPath());
    } catch (JsonParseException e) {
        log.warn("JsonParseException: {}", e.getMessage());

        if (out != null) {
            out.write(deco.print(fDoc.getPath(), fDoc.length(), "Json"));
            out.flush();
        }

        stats.setOk(false);
        FileLogger.error(BASE_NAME, "JsonParseException ''{0}''", doc.getPath());
    } finally {
        IOUtils.closeQuietly(fisContent);
    }

    return stats;
}