Example usage for com.liferay.portal.util HttpImpl getHostConfiguration

List of usage examples for com.liferay.portal.util HttpImpl getHostConfiguration

Introduction

In this page you can find the example usage for com.liferay.portal.util HttpImpl getHostConfiguration.

Prototype

@Deprecated
public HostConfiguration getHostConfiguration(String location) throws IOException 

Source Link

Usage

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 .ja v a 2  s.  c  om*/

    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.liferay.portlet.rss.util.RSSWebCacheItem.java

License:Open Source License

public Object convert(String key) throws WebCacheException {
    SyndFeed feed = null;/*from  w w w .ja  v a2s  .c  om*/

    try {

        // com.liferay.portal.kernel.util.HttpUtil will break the connection
        // if it spends more than 5 seconds looking up a location. However,
        // German umlauts do not get encoded correctly. This may be a bug
        // with commons-httpclient or with how FeedParser uses
        // java.io.Reader.

        // Use http://xml.newsisfree.com/feeds/29/629.xml and
        // http://test.domosoft.com/up/RSS to test if German umlauts show
        // up correctly.

        /*Reader reader = new StringReader(
           new String(HttpUtil.URLtoByteArray(_url)));
                
        channel = FeedParser.parse(builder, reader);*/

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

        HostConfiguration hostConfiguration = httpImpl.getHostConfiguration(_url);

        HttpClient httpClient = httpImpl.getClient(hostConfiguration);

        httpImpl.proxifyState(httpClient.getState(), hostConfiguration);

        HttpClientParams httpClientParams = httpClient.getParams();

        httpClientParams.setConnectionManagerTimeout(PropsValues.RSS_CONNECTION_TIMEOUT);
        httpClientParams.setSoTimeout(PropsValues.RSS_CONNECTION_TIMEOUT);

        GetMethod getMethod = new GetMethod(_url);

        httpClient.executeMethod(hostConfiguration, getMethod);

        SyndFeedInput input = new SyndFeedInput();

        feed = input.build(new XmlReader(getMethod.getResponseBodyAsStream()));
    } catch (Exception e) {
        throw new WebCacheException(_url + " " + e.toString());
    }

    return feed;
}

From source file:com.liferay.portlet.softwarecatalog.service.impl.SCProductVersionLocalServiceImpl.java

License:Open Source License

protected void testDirectDownloadURL(String directDownloadURL) throws PortalException {

    try {/*from   w  w w  . j a  v  a  2s . co m*/
        HttpImpl httpImpl = (HttpImpl) HttpUtil.getHttp();

        HostConfiguration hostConfiguration = httpImpl.getHostConfiguration(directDownloadURL);

        HttpClient httpClient = httpImpl.getClient(hostConfiguration);

        GetMethod getFileMethod = new GetMethod(directDownloadURL);

        int responseCode = httpClient.executeMethod(hostConfiguration, getFileMethod);

        if (responseCode != HttpServletResponse.SC_OK) {
            throw new UnavailableProductVersionDirectDownloadURLException();
        }
    } catch (Exception e) {
        throw new UnavailableProductVersionDirectDownloadURLException();
    }
}