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

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

Introduction

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

Prototype

@Override
public int getStatusCode() 

Source Link

Document

Returns the response status code.

Usage

From source file:org.opensaml.util.resource.HttpResource.java

/** {@inheritDoc} */
public DateTime getLastModifiedTime() throws ResourceException {
    HeadMethod headMethod = new HeadMethod(resourceUrl);

    try {/*from   w  w  w. j  av a 2  s.c  om*/
        httpClient.executeMethod(headMethod);
        if (headMethod.getStatusCode() != HttpStatus.SC_OK) {
            throw new ResourceException("Unable to retrieve resource URL " + resourceUrl
                    + ", received HTTP status code " + headMethod.getStatusCode());
        }
        Header lastModifiedHeader = headMethod.getResponseHeader("Last-Modified");
        if (lastModifiedHeader != null) {
            HeaderElement[] elements = lastModifiedHeader.getElements();
            if (elements.length > 0) {
                long lastModifiedTime = DateUtil.parseDate(elements[0].getValue()).getTime();
                return new DateTime(lastModifiedTime);
            }
        }

        return new DateTime();
    } catch (IOException e) {
        throw new ResourceException("Unable to contact resource URL: " + resourceUrl, e);
    } catch (DateParseException e) {
        throw new ResourceException("Unable to parse last modified date for resource:" + resourceUrl, e);
    }
}

From source file:org.osaf.caldav4j.CalDAVCalendarCollection.java

protected String getETag(HttpClient httpClient, String path) throws CalDAV4JException {
    HeadMethod headMethod = new HeadMethod(path);

    try {/*from   w  w  w.j  a va  2 s.  c om*/
        httpClient.executeMethod(hostConfiguration, headMethod);
        int statusCode = headMethod.getStatusCode();

        if (statusCode == CaldavStatus.SC_NOT_FOUND) {
            throw new ResourceNotFoundException(ResourceNotFoundException.IdentifierType.PATH, path);
        }

        if (statusCode != CaldavStatus.SC_OK) {
            throw new CalDAV4JException(
                    "Unexpected Status returned from Server: " + headMethod.getStatusCode());
        }
    } catch (IOException e) {
        throw new CalDAV4JException("Problem executing get method", e);
    }

    Header h = headMethod.getResponseHeader("ETag");
    String etag = null;
    if (h != null) {
        etag = h.getValue();
    }
    return etag;
}

From source file:org.osaf.caldav4j.CalDAVCollection.java

/**
 * retrieve etags using HEAD /path/to/resource.ics
 * /*from ww w . jav a 2s .c  om*/
 * @param httpClient
 * @param path
 * @return
 * @throws CalDAV4JException
 */
protected String getETag(HttpClient httpClient, String path) throws CalDAV4JException {
    HeadMethod headMethod = new HeadMethod(path);

    try {
        httpClient.executeMethod(hostConfiguration, headMethod);
        int statusCode = headMethod.getStatusCode();

        if (statusCode == CaldavStatus.SC_NOT_FOUND) {
            throw new ResourceNotFoundException(ResourceNotFoundException.IdentifierType.PATH, path);
        }

        if (statusCode != CaldavStatus.SC_OK) {
            throw new CalDAV4JException(
                    "Unexpected Status returned from Server: " + headMethod.getStatusCode());
        }
    } catch (IOException e) {
        String message = hostConfiguration.getHostURL() + headMethod.getPath();
        throw new CalDAV4JException("Problem executing HEAD method on: " + message, e);
    }

    Header h = headMethod.getResponseHeader(HEADER_ETAG);
    String etag = null;
    if (h != null) {
        etag = h.getValue();
    }
    return etag;
}

From source file:org.osaf.caldav4j.CalDAVCollection.java

public int testConnection(HttpClient httpClient) throws CalDAV4JException {
    HeadMethod method = new HeadMethod();
    method.setPath(getCalendarCollectionRoot());
    try {/*  ww w .ja  v  a2s.  c  o  m*/
        httpClient.executeMethod(hostConfiguration, method);
    } catch (Exception e) {
        throw new CalDAV4JException(e.getMessage(), new Throwable(e.getCause()));
    }

    switch (method.getStatusCode()) {
    case CaldavStatus.SC_OK:
        break;
    default:
        throw new BadStatusException(method.getStatusCode(), method.getName(), getCalendarCollectionRoot());
    }
    return method.getStatusCode();
}

From source file:org.silverpeas.openoffice.windows.webdav.WebdavManager.java

private boolean isFileExist(String url) throws IOException {
    HeadMethod method = new HeadMethod(url);
    client.executeMethod(method);//from   w  w  w . ja v  a 2  s  .c  om
    return method.getStatusCode() == HTTP_OK;
}

From source file:org.structr.function.UiFunction.java

protected GraphObjectMap headFromUrl(final ActionContext ctx, final String requestUrl, final String username,
        final String password) throws IOException, FrameworkException {

    final HttpClientParams params = new HttpClientParams(HttpClientParams.getDefaultParams());
    final HttpClient client = new HttpClient(params);
    final HeadMethod headMethod = new HeadMethod(requestUrl);

    if (username != null && password != null) {

        Credentials defaultcreds = new UsernamePasswordCredentials(username, password);
        client.getState().setCredentials(AuthScope.ANY, defaultcreds);
        client.getParams().setAuthenticationPreemptive(true);

        headMethod.setDoAuthentication(true);
    }//from ww  w  . j av a2  s .com

    headMethod.addRequestHeader("Connection", "close");
    // Don't follow redirects automatically, return status code 302 etc. instead
    headMethod.setFollowRedirects(false);

    // add request headers from context
    for (final Map.Entry<String, String> header : ctx.getHeaders().entrySet()) {
        headMethod.addRequestHeader(header.getKey(), header.getValue());
    }

    client.executeMethod(headMethod);

    final GraphObjectMap response = new GraphObjectMap();
    response.setProperty(new IntProperty("status"), headMethod.getStatusCode());
    response.setProperty(new StringProperty("headers"), extractHeaders(headMethod.getResponseHeaders()));

    return response;

}

From source file:typoscript.TypoScriptPluginOptions.java

/**
 * Checks to ensure all fields are filled out and that login credentials check out
 * /*from  w w  w.  j  av a2 s.  c o  m*/
 * @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;

}