Example usage for org.apache.commons.httpclient.params HttpMethodParams USER_AGENT

List of usage examples for org.apache.commons.httpclient.params HttpMethodParams USER_AGENT

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.params HttpMethodParams USER_AGENT.

Prototype

String USER_AGENT

To view the source code for org.apache.commons.httpclient.params HttpMethodParams USER_AGENT.

Click Source Link

Usage

From source file:org.red5.server.service.Installer.java

/**
 * Installs a given application.//from  ww  w.  j  a  v  a2  s .  co  m
 * 
 * @param applicationWarName app war name
 * @return true if installed; false otherwise
 */
public boolean install(String applicationWarName) {
    IConnection conn = Red5.getConnectionLocal();

    boolean result = false;

    //strip everything except the applications name
    String application = applicationWarName.substring(0, applicationWarName.indexOf('-'));
    log.debug("Application name: {}", application);

    //get webapp location
    String webappsDir = System.getProperty("red5.webapp.root");
    log.debug("Webapp folder: {}", webappsDir);

    //setup context
    String contextPath = '/' + application;
    String contextDir = webappsDir + contextPath;

    //verify this is a unique app
    File appDir = new File(webappsDir, application);
    if (appDir.exists()) {
        if (appDir.isDirectory()) {
            log.debug("Application directory exists");
        } else {
            log.warn("Application destination is not a directory");
        }

        ServiceUtils.invokeOnConnection(conn, "onAlert",
                new Object[] { String.format(
                        "Application %s already installed, please un-install before attempting another install",
                        application) });
    } else {
        //use the system temp directory for moving files around
        String srcDir = System.getProperty("java.io.tmpdir");
        log.debug("Source directory: {}", srcDir);
        //look for archive containing application (war, zip, etc..)
        File dir = new File(srcDir);
        if (!dir.exists()) {
            log.warn("Source directory not found");
            //use another directory
            dir = new File(System.getProperty("red5.root"), "/webapps/installer/WEB-INF/cache");
            if (!dir.exists()) {
                if (dir.mkdirs()) {
                    log.info("Installer cache directory created");
                }
            }
        } else {
            if (!dir.isDirectory()) {
                log.warn("Source directory is not a directory");
            }
        }
        //get a list of temp files
        File[] files = dir.listFiles();
        for (File f : files) {
            String fileName = f.getName();
            if (fileName.equals(applicationWarName)) {
                log.debug("File found matching application name");
                result = true;
                break;
            }
        }
        dir = null;

        //if the file was not found then download it
        if (!result) {
            // create a singular HttpClient object
            HttpClient client = new HttpClient();

            // set the proxy (WT)
            if ((System.getProperty("http.proxyHost") != null)
                    && (System.getProperty("http.proxyPort") != null)) {
                HostConfiguration config = client.getHostConfiguration();
                config.setProxy(System.getProperty("http.proxyHost").toString(),
                        Integer.parseInt(System.getProperty("http.proxyPort")));
            }

            // establish a connection within 5 seconds
            client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
            //get the params for the client
            HttpClientParams params = client.getParams();
            params.setParameter(HttpMethodParams.USER_AGENT, userAgent);
            params.setParameter(HttpMethodParams.STRICT_TRANSFER_ENCODING, Boolean.TRUE);

            //try the wav version first
            HttpMethod method = new GetMethod(applicationRepositoryUrl + applicationWarName);
            //we dont want any transformation - RFC2616
            method.addRequestHeader("Accept-Encoding", "identity");
            //follow any 302's although there shouldnt be any
            method.setFollowRedirects(true);
            FileOutputStream fos = null;
            // execute the method
            try {
                int code = client.executeMethod(method);
                log.debug("HTTP response code: {}", code);
                //create output file
                fos = new FileOutputStream(srcDir + '/' + applicationWarName);
                log.debug("Writing response to {}/{}", srcDir, applicationWarName);

                // have to receive the response as a byte array.  This has the advantage of writing to the filesystem
                // faster and it also works on macs ;)
                byte[] buf = method.getResponseBody();
                fos.write(buf);
                fos.flush();

                result = true;
            } catch (HttpException he) {
                log.error("Http error connecting to {}", applicationRepositoryUrl, he);
            } catch (IOException ioe) {
                log.error("Unable to connect to {}", applicationRepositoryUrl, ioe);
            } finally {
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                    }
                }
                if (method != null) {
                    method.releaseConnection();
                }
            }
        }

        //if we've found or downloaded the war
        if (result) {
            //get the webapp loader
            LoaderMBean loader = getLoader();
            if (loader != null) {
                //un-archive it to app dir
                FileUtil.unzip(srcDir + '/' + applicationWarName, contextDir);
                //load and start the context
                loader.startWebApplication(application);
            } else {
                //just copy the war to the webapps dir
                try {
                    FileUtil.moveFile(srcDir + '/' + applicationWarName,
                            webappsDir + '/' + application + ".war");
                    ServiceUtils.invokeOnConnection(conn, "onAlert",
                            new Object[] { String.format(
                                    "Application %s will not be available until container is restarted",
                                    application) });
                } catch (IOException e) {
                }
            }
        }

        ServiceUtils.invokeOnConnection(conn, "onAlert", new Object[] { String.format("Application %s was %s",
                application, (result ? "installed" : "not installed")) });

    }
    appDir = null;

    return result;
}

From source file:org.sakaiproject.entitybroker.util.http.HttpRESTUtils.java

/**
 * Generates a reusable http client wrapper which can be given to {@link #fireRequest(HttpClientWrapper, String, Method, Map, Object, boolean)}
 * as an efficiency mechanism//from  w  w w .j a va  2  s  .  c  o m
 * 
 * @param multiThreaded true if you want to allow the client to run in multiple threads
 * @param idleConnectionTimeout if this is 0 then it will use the defaults, otherwise connections will be timed out after this long (ms)
 * @param cookies to send along with every request from this client
 * @return the reusable http client wrapper
 */
public static HttpClientWrapper makeReusableHttpClient(boolean multiThreaded, int idleConnectionTimeout,
        Cookie[] cookies) {
    HttpClientWrapper wrapper;
    HttpClient client;
    MultiThreadedHttpConnectionManager connectionManager = null;
    if (multiThreaded) {
        connectionManager = new MultiThreadedHttpConnectionManager();
        client = new HttpClient(connectionManager);
    } else {
        client = new HttpClient();
    }
    if (idleConnectionTimeout <= 0) {
        idleConnectionTimeout = 5000;
    }
    client.getHttpConnectionManager().closeIdleConnections(idleConnectionTimeout);
    client.getHttpConnectionManager().getParams().setConnectionTimeout(idleConnectionTimeout);
    // create the initial state
    HttpState initialState = new HttpState();
    if (cookies != null && cookies.length > 0) {
        for (int i = 0; i < cookies.length; i++) {
            Cookie c = cookies[i];
            org.apache.commons.httpclient.Cookie mycookie = new org.apache.commons.httpclient.Cookie(
                    c.getDomain(), c.getName(), c.getValue(), c.getPath(), c.getMaxAge(), c.getSecure());
            initialState.addCookie(mycookie);
        }
        client.setState(initialState);
    }
    // set some defaults
    client.getParams().setParameter(HttpMethodParams.USER_AGENT,
            "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1");
    client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    client.getParams().setBooleanParameter(HttpMethodParams.SINGLE_COOKIE_HEADER, true);
    wrapper = new HttpClientWrapper(client, connectionManager, initialState);
    return wrapper;
}

From source file:org.xwiki.rendering.internal.transformation.linkchecker.DefaultHTTPChecker.java

@Override
public void initialize() throws InitializationException {
    this.httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());

    // Set our user agent to be a good citizen.
    this.httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT, "XWikiLinkChecker");
    // Ignore cookies since this can cause errors in logs and we don't need cookies when checking sites.
    this.httpClient.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
}

From source file:xbl.Session.java

private void init() {
    client = new HttpClient();
    client.getParams().setParameter(HttpMethodParams.USER_AGENT,
            "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.3) Gecko/2008092414 Firefox/3.0.3");
}