Example usage for org.apache.commons.httpclient.methods HeadMethod getStatusText

List of usage examples for org.apache.commons.httpclient.methods HeadMethod getStatusText

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods HeadMethod getStatusText.

Prototype

@Override
public String getStatusText() 

Source Link

Document

Returns the status text (or "reason phrase") associated with the latest response.

Usage

From source file:net.sf.ufsc.http.HttpFile.java

/**
 * @see net.sf.ufsc.File#exists()//from   w ww  . j  av  a  2 s .  c o  m
 */
public boolean exists() throws java.io.IOException {
    HeadMethod method = new HeadMethod(this.uri.toString());

    int status = this.client.executeMethod(method);

    if (status == HttpStatus.SC_OK) {
        return true;
    } else if (status == HttpStatus.SC_NOT_FOUND) {
        return false;
    } else {
        throw new IOException(method.getStatusText());
    }
}

From source file:fr.jayasoft.ivy.url.HttpClientHandler.java

public URLInfo getURLInfo(URL url, int timeout) {
    HeadMethod head = null;
    try {//w  ww .  j a  va 2s .c o m
        head = doHead(url, timeout);
        int status = head.getStatusCode();
        head.releaseConnection();
        if (status == HttpStatus.SC_OK) {
            return new URLInfo(true, getResponseContentLength(head), getLastModified(head));
        }
        if (status == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) {
            Message.error("Your proxy requires authentication.");
        } else if (String.valueOf(status).startsWith("4")) {
            Message.verbose("CLIENT ERROR: " + head.getStatusText() + " url=" + url);
        } else if (String.valueOf(status).startsWith("5")) {
            Message.warn("SERVER ERROR: " + head.getStatusText() + " url=" + url);
        }
        Message.debug("HTTP response status: " + status + "=" + head.getStatusText() + " url=" + url);
    } catch (HttpException e) {
        Message.error("HttpClientHandler: " + e.getMessage() + ":" + e.getReasonCode() + "=" + e.getReason()
                + " url=" + url);
    } catch (UnknownHostException e) {
        Message.warn("Host " + e.getMessage() + " not found. url=" + url);
        Message.info(
                "You probably access the destination server through a proxy server that is not well configured.");
    } catch (IOException e) {
        Message.error("HttpClientHandler: " + e.getMessage() + " url=" + url);
    } finally {
        if (head != null) {
            head.releaseConnection();
        }
    }
    return UNAVAILABLE;
}

From source file:com.zimbra.cs.store.triton.TritonIncomingBlob.java

@Override
protected long getRemoteSize() throws IOException {
    outStream.flush();// w w w  .j a  va  2s .  co m
    HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    HeadMethod head = new HeadMethod(baseUrl + uploadUrl);
    ZimbraLog.store.info("heading %s", head.getURI());
    try {
        head.addRequestHeader(TritonHeaders.SERVER_TOKEN, serverToken.getToken());
        int statusCode = HttpClientUtil.executeMethod(client, head);
        if (statusCode == HttpStatus.SC_OK) {
            String contentLength = head.getResponseHeader(TritonHeaders.CONTENT_LENGTH).getValue();
            long remoteSize = -1;
            try {
                remoteSize = Long.valueOf(contentLength);
            } catch (NumberFormatException nfe) {
                throw new IOException("Content length can't be parsed to Long", nfe);
            }
            return remoteSize;
        } else {
            ZimbraLog.store.error("failed with code %d response: %s", statusCode,
                    head.getResponseBodyAsString());
            throw new IOException("unable to head blob " + statusCode + ":" + head.getStatusText(), null);
        }
    } finally {
        head.releaseConnection();
    }
}

From source file:org.apache.maven.proxy.config.HttpRepoConfiguration.java

protected ProxyArtifact getMetaInformationInternal(String url) {
    try {/* www  .  j  a va  2s. c om*/
        String fullUrl = getUrl() + url;
        LOGGER.info(this + ": Checking last modified time for " + fullUrl);
        HttpClient client = createHttpClient();
        HeadMethod method = new HeadMethod(fullUrl);
        client.executeMethod(method);

        if (method.getStatusCode() == HttpServletResponse.SC_NOT_FOUND) {
            return null;
        }

        if (method.getStatusCode() != HttpServletResponse.SC_OK) {
            LOGGER.info(this + ": Unable to find " + fullUrl + " because of [" + method.getStatusCode() + "] = "
                    + method.getStatusText());
            return null;
        }

        long lastModified = getLastModified(method);
        long size = getContentLength(method);
        ProxyArtifact snapshot = new ProxyArtifact(this, url);
        snapshot.setLastModified(lastModified);
        snapshot.setSize(size);
        return snapshot;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.webdav.ant.Utils.java

/**
 * Returns <code>true</code> if the resource given as URL does exist.
 * @param client/* ww w.ja  v a  2  s .co  m*/
 * @param httpURL
 * @return <code>true</code>if the resource exists
 * @throws IOException
 * @throws HttpException
 */
public static boolean resourceExists(HttpClient client, HttpURL httpURL) throws IOException, HttpException {
    HeadMethod head = new HeadMethod(httpURL.getEscapedURI());
    head.setFollowRedirects(true);
    int status = client.executeMethod(head);

    switch (status) {
    case WebdavStatus.SC_OK:
        return true;
    case WebdavStatus.SC_NOT_FOUND:
        return false;
    default:
        HttpException ex = new HttpException();
        ex.setReasonCode(status);
        ex.setReason(head.getStatusText());
        throw ex;
    }
}

From source file:org.bibsonomy.rest.client.worker.impl.HeadWorker.java

@Override
protected Reader readResponse(HeadMethod method) throws IOException, ErrorPerformingRequestException {
    return new StringReader(method.getStatusText());
}

From source file:typoscript.TypoScriptPluginOptions.java

/**
 * Checks to ensure all fields are filled out and that login credentials check out
 * //from ww  w  .  j  a v a2  s .com
 * @param reportOK Whether to report on success (false if this is a passive check before adding)
 * @return true if everything checks out
 */
private boolean test(boolean reportOK) {
    URL urlFull = null;
    URL urlBase = null;

    if (txtURL.getText().length() == 0) {
        JOptionPane.showMessageDialog(this, "You must specify a URL", "URL Field Blank",
                JOptionPane.ERROR_MESSAGE);
        return false;
    } else {
        try {
            String typeNum = jEdit.getProperty(TypoScriptPlugin.PROPERTY_PREFIX + "typenum");
            urlFull = new URL(txtURL.getText() + "index.php?type=" + typeNum);
            urlBase = new URL(txtURL.getText());
        } catch (MalformedURLException e) {
            JOptionPane.showMessageDialog(this,
                    "The URL you entered was not valid.\nPlease ensure it starts with http:// or https:// and contains no spaces or other characters that would normally be disallowed in a URL",
                    "Invalid URL", JOptionPane.ERROR_MESSAGE);
            return false;
        }

        if (!txtURL.getText().endsWith("/")) {
            JOptionPane.showMessageDialog(this,
                    "The URL you entered did not end with a '/' character. This is required.\nIf you would normally login at http://typo3.example.com/typo3/, then you need to enter http://typo3.example.com/",
                    "Invalid URL", JOptionPane.ERROR_MESSAGE);
            return false;
        }
    }
    if (txtUser.getText().length() == 0) {
        JOptionPane.showMessageDialog(this,
                "You must specify a username, even if you have some alternative authentication scheme (make one up)",
                "Username Field Blank", JOptionPane.ERROR_MESSAGE);
        return false;
    }
    if (txtPass.getPassword().length == 0) {
        JOptionPane.showMessageDialog(this, "You must specify a password", "Password Field Blank",
                JOptionPane.ERROR_MESSAGE);
        return false;
    }

    // First stage checks out. Disable the buttons and check the provided information
    disableButtons();

    this.pack(); // resize window

    try {
        HttpClient client = new HttpClient();
        HeadMethod head = new HeadMethod(urlFull.toString());
        client.executeMethod(head);

        //head.execute()
        if (head.getStatusCode() != HttpStatus.SC_OK) {
            JOptionPane.showMessageDialog(this, "Received a non-200 response code from the server: "
                    + head.getStatusCode() + "(" + head.getStatusText()
                    + ")\nPlease ensure there are no rewrite rules or redirects in the way.\nI tried URL: "
                    + urlFull.toString(), "Non-200 response", JOptionPane.ERROR_MESSAGE);
            resetButtonState();
            return false;
        }
        if (head.getResponseHeader("X-jeditvfs-present") == null) {
            JOptionPane.showMessageDialog(this,
                    "The jEdit VFS extension doesn't appear to be installed on the remote site.\nPlease install the 'jeditvfs' plugin from the TYPO3 extension repository and try again.\nIt's also possible the URL didn't point to the right place.\nYou  shoud be pointing to the root of the FRONTEND of your site.\nI tried: "
                            + urlFull.toString(),
                    "jeditvfs extension not located", JOptionPane.ERROR_MESSAGE);
            resetButtonState();
            return false;
        }
    } catch (IOException e) {
        JOptionPane.showMessageDialog(this,
                "An I/O error occured while testing the site.\nPlease check your internet connection. Details below:\n"
                        + e.toString(),
                "IOException", JOptionPane.ERROR_MESSAGE);
        resetButtonState();
        return false;
    }

    // That worked. Now we need to construct a real T3Site object and perform an authentication check (and get the site name in the process)
    curSite = new T3Site(urlBase, urlFull, txtUser.getText(), new String(txtPass.getPassword()),
            chkClearCache.isSelected());

    try {
        curSite.setName(curSite.getWorker().getSiteTitle());
    } catch (IOException e) {
        JOptionPane.showMessageDialog(this,
                "An I/O error occured while testing the site.\nPlease check your internet connection. Details below:\n"
                        + e.toString(),
                "IOException", JOptionPane.ERROR_MESSAGE);
        resetButtonState();
        return false;
    } catch (XmlRpcException e) {
        // Probably an authentication failure.
        if (e.code == RemoteCallWorker.JEDITVFS_ERROR_AUTHFAIL) {
            JOptionPane.showMessageDialog(this,
                    "Authentication failed.\nPlease make sure you typed your username and password correctly.\nNote also that you require an admin-level backend account to use this system.\nIf you get this failure and are using an exotic authentication scheme, please contact the developer of this plugin to report a bug.\nServer said: "
                            + e.getMessage(),
                    "Backend authentication failed", JOptionPane.ERROR_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(this,
                    "The server returned an unexpected response in reply to an authentication test\nServer said: "
                            + e.getMessage(),
                    "Unexpected XML-RPC exception", JOptionPane.ERROR_MESSAGE);
        }
        resetButtonState();
        return false;
    }

    if (reportOK) {
        JOptionPane.showMessageDialog(this, "All OK", "All tests passed", JOptionPane.INFORMATION_MESSAGE);
    }
    resetButtonState();
    return true;

}