Example usage for com.liferay.portal.kernel.util FileUtil copyFile

List of usage examples for com.liferay.portal.kernel.util FileUtil copyFile

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.util FileUtil copyFile.

Prototype

public static void copyFile(String source, String destination) throws IOException 

Source Link

Usage

From source file:com.liferay.marketplace.app.manager.web.internal.portlet.MarketplaceAppManagerPortlet.java

License:Open Source License

public void installLocalApp(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

    UploadPortletRequest uploadPortletRequest = _portal.getUploadPortletRequest(actionRequest);

    String fileName = GetterUtil.getString(uploadPortletRequest.getFileName("file"));

    File file = uploadPortletRequest.getFile("file");

    byte[] bytes = FileUtil.getBytes(file);

    if (ArrayUtil.isEmpty(bytes)) {
        SessionErrors.add(actionRequest, UploadException.class.getName());
    } else if (!fileName.endsWith(".jar") && !fileName.endsWith(".lpkg") && !fileName.endsWith(".war")) {

        throw new FileExtensionException();
    } else {//from   www .  ja v  a 2s . co m
        String deployDir = PrefsPropsUtil.getString(PropsKeys.AUTO_DEPLOY_DEPLOY_DIR);

        FileUtil.copyFile(file.toString(), deployDir + StringPool.SLASH + fileName);

        SessionMessages.add(actionRequest, "pluginUploaded");
    }

    sendRedirect(actionRequest, actionResponse);
}

From source file:com.liferay.marketplace.appmanager.portlet.AppManagerPortlet.java

License:Open Source License

public void installApp(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

    UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(actionRequest);

    String installMethod = ParamUtil.getString(uploadPortletRequest, "installMethod");

    if (installMethod.equals("local")) {
        String fileName = GetterUtil.getString(uploadPortletRequest.getFileName("file"));

        File file = uploadPortletRequest.getFile("file");

        byte[] bytes = FileUtil.getBytes(file);

        if (ArrayUtil.isEmpty(bytes)) {
            SessionErrors.add(actionRequest, UploadException.class.getName());
        } else {//from  w w w. j ava  2 s.c o  m
            String deployDir = PrefsPropsUtil.getString(PropsKeys.AUTO_DEPLOY_DEPLOY_DIR);

            FileUtil.copyFile(file.toString(), deployDir + StringPool.SLASH + fileName);

            SessionMessages.add(actionRequest, "pluginUploaded");
        }
    } else {
        try {
            String url = ParamUtil.getString(uploadPortletRequest, "url");

            URL urlObj = new URL(url);

            String host = urlObj.getHost();

            if (host.endsWith("sf.net") || host.endsWith("sourceforge.net")) {

                doInstallSourceForgeApp(urlObj.getPath(), uploadPortletRequest, actionRequest);
            } else {
                doInstallRemoteApp(url, uploadPortletRequest, actionRequest, true);
            }
        } catch (MalformedURLException murle) {
            SessionErrors.add(actionRequest, "invalidUrl", murle);
        }
    }

    String redirect = ParamUtil.getString(uploadPortletRequest, "redirect");

    actionResponse.sendRedirect(redirect);
}

From source file:com.liferay.portlet.documentlibrary.store.FileSystemStore.java

License:Open Source License

@Override
public void copyFileVersion(long companyId, long repositoryId, String fileName, String fromVersionLabel,
        String toVersionLabel) throws PortalException, SystemException {

    File fromFileNameVersionFile = getFileNameVersionFile(companyId, repositoryId, fileName, fromVersionLabel);

    File toFileNameVersionFile = getFileNameVersionFile(companyId, repositoryId, fileName, toVersionLabel);

    if (toFileNameVersionFile.exists()) {
        throw new DuplicateFileException(toFileNameVersionFile.getPath());
    }//from www.  j a  va 2 s .com

    try {
        toFileNameVersionFile.createNewFile();

        FileUtil.copyFile(fromFileNameVersionFile, toFileNameVersionFile);
    } catch (IOException ioe) {
        throw new SystemException(ioe);
    }
}

From source file:com.liferay.portlet.plugininstaller.action.InstallPluginAction.java

License:Open Source License

protected void localDeploy(ActionRequest actionRequest) throws Exception {
    UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(actionRequest);

    String fileName = null;/*  w w w.java  2 s. c  o m*/

    String deploymentContext = ParamUtil.getString(actionRequest, "deploymentContext");

    if (Validator.isNotNull(deploymentContext)) {
        fileName = BaseDeployer.DEPLOY_TO_PREFIX + deploymentContext + ".war";
    } else {
        fileName = GetterUtil.getString(uploadPortletRequest.getFileName("file"));

        int pos = fileName.lastIndexOf(CharPool.PERIOD);

        if (pos != -1) {
            deploymentContext = fileName.substring(0, pos);
        }
    }

    File file = uploadPortletRequest.getFile("file");

    byte[] bytes = FileUtil.getBytes(file);

    if ((bytes == null) || (bytes.length == 0)) {
        SessionErrors.add(actionRequest, UploadException.class.getName());

        return;
    }

    try {
        PluginPackageUtil.registerPluginPackageInstallation(deploymentContext);

        String source = file.toString();

        String deployDir = PrefsPropsUtil.getString(PropsKeys.AUTO_DEPLOY_DEPLOY_DIR,
                PropsValues.AUTO_DEPLOY_DEPLOY_DIR);

        String destination = deployDir + StringPool.SLASH + fileName;

        FileUtil.copyFile(source, destination);

        SessionMessages.add(actionRequest, "pluginUploaded");
    } finally {
        PluginPackageUtil.endPluginPackageInstallation(deploymentContext);
    }
}

From source file:com.liferay.portlet.plugininstaller.action.InstallPluginAction.java

License:Open Source License

protected int remoteDeploy(String url, URL urlObj, ActionRequest actionRequest, boolean failOnError)
        throws Exception {

    int responseCode = HttpServletResponse.SC_OK;

    GetMethod getMethod = null;// ww  w  .j  a  va  2s.  co  m

    String deploymentContext = ParamUtil.getString(actionRequest, "deploymentContext");

    try {
        HttpImpl httpImpl = (HttpImpl) HttpUtil.getHttp();

        HostConfiguration hostConfiguration = httpImpl.getHostConfiguration(url);

        HttpClient httpClient = httpImpl.getClient(hostConfiguration);

        getMethod = new GetMethod(url);

        String fileName = null;

        if (Validator.isNotNull(deploymentContext)) {
            fileName = BaseDeployer.DEPLOY_TO_PREFIX + deploymentContext + ".war";
        } else {
            fileName = url.substring(url.lastIndexOf(CharPool.SLASH) + 1);

            int pos = fileName.lastIndexOf(CharPool.PERIOD);

            if (pos != -1) {
                deploymentContext = fileName.substring(0, pos);
            }
        }

        PluginPackageUtil.registerPluginPackageInstallation(deploymentContext);

        responseCode = httpClient.executeMethod(hostConfiguration, getMethod);

        if (responseCode != HttpServletResponse.SC_OK) {
            if (failOnError) {
                SessionErrors.add(actionRequest, "errorConnectingToUrl",
                        new Object[] { String.valueOf(responseCode) });
            }

            return responseCode;
        }

        long contentLength = getMethod.getResponseContentLength();

        String progressId = ParamUtil.getString(actionRequest, Constants.PROGRESS_ID);

        ProgressInputStream pis = new ProgressInputStream(actionRequest, getMethod.getResponseBodyAsStream(),
                contentLength, progressId);

        String deployDir = PrefsPropsUtil.getString(PropsKeys.AUTO_DEPLOY_DEPLOY_DIR,
                PropsValues.AUTO_DEPLOY_DEPLOY_DIR);

        String tmpFilePath = deployDir + StringPool.SLASH + _DOWNLOAD_DIR + StringPool.SLASH + fileName;

        File tmpFile = new File(tmpFilePath);

        if (!tmpFile.getParentFile().exists()) {
            tmpFile.getParentFile().mkdirs();
        }

        FileOutputStream fos = new FileOutputStream(tmpFile);

        try {
            pis.readAll(fos);

            if (_log.isInfoEnabled()) {
                _log.info("Downloaded plugin from " + urlObj + " has " + pis.getTotalRead() + " bytes");
            }
        } finally {
            pis.clearProgress();
        }

        getMethod.releaseConnection();

        if (pis.getTotalRead() > 0) {
            String destination = deployDir + StringPool.SLASH + fileName;

            File destinationFile = new File(destination);

            boolean moved = FileUtil.move(tmpFile, destinationFile);

            if (!moved) {
                FileUtil.copyFile(tmpFile, destinationFile);
                FileUtil.delete(tmpFile);
            }

            SessionMessages.add(actionRequest, "pluginDownloaded");
        } else {
            if (failOnError) {
                SessionErrors.add(actionRequest, UploadException.class.getName());
            }

            responseCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
        }
    } catch (MalformedURLException murle) {
        SessionErrors.add(actionRequest, "invalidUrl", murle);
    } catch (IOException ioe) {
        SessionErrors.add(actionRequest, "errorConnectingToUrl", ioe);
    } finally {
        if (getMethod != null) {
            getMethod.releaseConnection();
        }

        PluginPackageUtil.endPluginPackageInstallation(deploymentContext);
    }

    return responseCode;
}

From source file:com.sqli.liferay.imex.portal.kernel.deploy.auto.ImExAutoDeployListener.java

License:Open Source License

public void deploy(File file, String context) throws AutoDeployException {

    Properties config = new Properties();
    try {/*  w w w .  ja  v  a  2 s  .co m*/
        FileReader fileReader = new FileReader(file);
        config.load(fileReader);
        fileReader.close();
    } catch (FileNotFoundException e) {
        throw new AutoDeployException(e);
    } catch (IOException e) {
        throw new AutoDeployException(e);
    }

    String fileName = file.getName().toLowerCase();
    if (fileName.endsWith(".import.properties")) {

        LOG.info("Import started for " + file);

        createTempFolder();

        try {
            File data = getImportDataDir(file);
            processImport(config, data);
        } catch (Exception e) {
            throw new AutoDeployException(e);
        } finally {
            deleteTempFolder();
        }

        LOG.info("Import finished for " + file);

    } else if (fileName.endsWith(".export.properties")) {

        LOG.info("Export started for " + file);

        File data = getExportDataDir(file);

        processExport(data, file);

        ZipUtils zipUtils = new ZipUtils(LOG);
        try {
            zipUtils.zipFiles(new File(data.getParent(), data.getName() + ".imex.zip"), data);
        } catch (IOException e) {
            throw new AutoDeployException();
        }

        LOG.info("Export finished for " + file);
    }

    try {
        FileUtil.copyFile(file, new File(file.getParentFile(), file.getName() + ".done"));
    } catch (IOException e) {
        LOG.error(e, e);
        throw new AutoDeployException();
    }
}

From source file:eu.ibacz.extlet.deploy.hot.ExtletHotDeployer.java

License:Open Source License

/**
 * Copies all extlet's jars specified by {@link ExtletHotDeployer#EXTLET_IMPL_JAR_NAME_KEY} into the portal's WEB-INF/lib directory.
 *///from  w  w  w  .  ja va  2  s. co m
private void copyExtletImplJar() throws HotDeployException {
    if (_log.isDebugEnabled()) {
        _log.debug("About to deploying the extlet impl.");
    }
    File[] extletImplJars = getExtletImplJars();

    for (File extletImplJar : extletImplJars) {
        File portalImplJar = getPortalImplJar(extletImplJar);
        if (_log.isDebugEnabled()) {
            _log.info("Deploying extlet impl " + extletImplJar.getName() + " from "
                    + event.getServletContextName());
        }
        FileUtil.copyFile(extletImplJar, portalImplJar);
        if (_log.isDebugEnabled()) {
            _log.info("Extlet Impl Jar " + extletImplJar.getName() + " deployed successfully.");
        }
    }
}

From source file:eu.ibacz.extlet.deploy.hot.ExtletHotDeployer.java

License:Open Source License

/**
 * Copies all extlet's jars specified by {@link ExtletHotDeployer#EXTLET_SERVICE_JAR_NAME_KEY} into the directory with portal-kernel.jar.
 *//*from www  .j a  v a  2  s. co m*/
private void copyExtletServiceJars() throws HotDeployException {
    if (_log.isDebugEnabled()) {
        _log.debug("About to start to deploy extlet services.");
    }
    File[] extletServiceJars = getExtletServiceJars();
    for (File extletServiceJar : extletServiceJars) {
        File portalServiceJar = getPortalServiceJar(extletServiceJar);
        if (_log.isDebugEnabled()) {
            _log.info("Deploying extlet service " + extletServiceJar.getAbsolutePath() + " to "
                    + portalServiceJar.getAbsolutePath());
        }
        FileUtil.copyFile(extletServiceJar, portalServiceJar);
        if (_log.isDebugEnabled()) {
            _log.info("Extlet Service Jar " + extletServiceJar.getName() + " deployed successfully.");
        }
    }
}