Example usage for org.apache.commons.httpclient HttpException getMessage

List of usage examples for org.apache.commons.httpclient HttpException getMessage

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpException getMessage.

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:org.xwiki.test.webdav.DefaultWebDAVTest.java

/**
 * Test making attachment./*  w  w w.j  av a  2s . c  om*/
 */
public void testMakingAttachment() {
    String spaceUrl = SPACES + "/TestSpace";
    String pageUrl = spaceUrl + "/TestPage";
    String attachmentUrl = pageUrl + "/attachment.txt";
    String attachmentContent = "Attachment Content";
    DeleteMethod deleteMethod = new DeleteMethod();
    deleteMethod.setDoAuthentication(true);
    MkcolMethod mkColMethod = new MkcolMethod();
    mkColMethod.setDoAuthentication(true);
    PutMethod putMethod = new PutMethod();
    putMethod.setDoAuthentication(true);
    GetMethod getMethod = new GetMethod();
    getMethod.setDoAuthentication(true);
    try {
        deleteMethod.setPath(spaceUrl);
        assertEquals(DavServletResponse.SC_NO_CONTENT, getHttpClient().executeMethod(deleteMethod));
        mkColMethod.setPath(spaceUrl);
        assertEquals(DavServletResponse.SC_CREATED, getHttpClient().executeMethod(mkColMethod));
        mkColMethod.setPath(pageUrl);
        assertEquals(DavServletResponse.SC_CREATED, getHttpClient().executeMethod(mkColMethod));
        getMethod.setPath(attachmentUrl);
        assertEquals(DavServletResponse.SC_NOT_FOUND, getHttpClient().executeMethod(getMethod));
        putMethod.setPath(attachmentUrl);
        putMethod.setRequestEntity(
                new InputStreamRequestEntity(new ByteArrayInputStream(attachmentContent.getBytes())));
        assertEquals(DavServletResponse.SC_CREATED, getHttpClient().executeMethod(putMethod));
        getMethod.setPath(attachmentUrl);
        assertEquals(DavServletResponse.SC_OK, getHttpClient().executeMethod(getMethod));
        assertEquals(attachmentContent, getMethod.getResponseBodyAsString());
        deleteMethod.setPath(attachmentUrl);
        assertEquals(DavServletResponse.SC_NO_CONTENT, getHttpClient().executeMethod(deleteMethod));
        deleteMethod.setPath(pageUrl);
        assertEquals(DavServletResponse.SC_NO_CONTENT, getHttpClient().executeMethod(deleteMethod));
        deleteMethod.setPath(spaceUrl);
        assertEquals(DavServletResponse.SC_NO_CONTENT, getHttpClient().executeMethod(deleteMethod));
    } catch (HttpException ex) {
        fail(ex.getMessage());
    } catch (IOException ex) {
        fail(ex.getMessage());
    }
}

From source file:org.zaproxy.zap.extension.betterfuzz.impl.http.HttpFuzzProcess.java

@Override
public FuzzResult fuzz(Fuzzer fuzz) {
    String tokenValue = null;//from  www .  j  a va 2s .  c  om
    HttpFuzzResult fuzzResult = new HttpFuzzResult();

    if (this.acsrfToken != null) {
        // This currently just supports a single token in one page
        // To support wizards etc need to loop back up the messages for previous tokens
        try {
            HttpMessage tokenMsg = this.acsrfToken.getMsg().cloneAll();
            httpSender.sendAndReceive(tokenMsg);

            // If we've got a token value here then the AntiCSRF extension must have been registered
            tokenValue = extAntiCSRF.getTokenValue(tokenMsg, acsrfToken.getName());

            if (showTokenRequests) {
                List<HttpMessage> tokenRequests = new ArrayList<>();
                tokenRequests.add(tokenMsg);
                fuzzResult.setTokenRequestMessages(tokenRequests);
            }
        } catch (HttpException e) {
            logger.error(e.getMessage(), e);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
    }

    String fuzzString;
    if (urlEncode) {
        fuzzString = encoder.getURLEncode(fuzz.getFuzzString());
    } else {
        fuzzString = fuzz.getFuzzString();
    }

    HttpMessage msg;
    try {
        // Inject the payload
        msg = (HttpMessage) fuzzableHttpMessage.fuzz(fuzzString);
    } catch (Exception e) {
        msg = ((HttpMessage) fuzzableHttpMessage.getMessage()).cloneRequest();
        msg.setNote(fuzz.getFuzzString());
        fuzzResult.setMessage(msg);

        fuzzResult.setState(State.ERROR);
        return fuzzResult;
    }

    msg.setNote(fuzz.getFuzzString());

    if (tokenValue != null) {
        // Replace token value - only supported in the body right now
        String replaced = msg.getRequestBody().toString();
        replaced = replaced.replace(encoder.getURLEncode(acsrfToken.getValue()),
                encoder.getURLEncode(tokenValue));
        msg.setRequestBody(replaced);
    }
    // Correct the content length for the above changes
    msg.getRequestHeader().setContentLength(msg.getRequestBody().length());

    try {
        httpSender.sendAndReceive(msg);

        if (fuzz.isInjected(msg.getResponseBody().toString())) {
            msg.setNote(fuzz.getMatchedString()); // TODO: This should probably be stored in the fuzzResult instead of the msg.
            fuzzResult.setState(State.INJECTED);
        }
    } catch (HttpException e) {
        logger.error(e.getMessage(), e);
        fuzzResult.setState(State.ERROR);
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
        fuzzResult.setState(State.ERROR);
    }

    fuzzResult.setMessage(msg);

    return fuzzResult;
}

From source file:org.zaproxy.zap.extension.fuzz.FuzzProcess.java

@Override
public void run() {
    String tokenValue = null;/*from  w ww  .j  av  a2  s.  co m*/
    for (FuzzerListener listener : listenerList) {
        listener.notifyFuzzProcessStarted(this);
    }

    if (this.acsrfToken != null) {
        // This currently just supports a single token in one page
        // To support wizards etc need to loop back up the messages for previous tokens
        try {
            HttpMessage tokenMsg = this.acsrfToken.getMsg().cloneAll();
            HttpSender httpSender = new HttpSender(connectionParam, true);

            httpSender.sendAndReceive(tokenMsg);

            // If we've got a token value here then the AntiCSRF extension must have been registered
            tokenValue = extAntiCSRF.getTokenValue(tokenMsg, acsrfToken.getName());

            this.tokenRequests.add(tokenMsg);

        } catch (HttpException e) {
            log.error(e.getMessage(), e);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }

    // Inject the payload
    try {

        String fuzzString;
        if (urlEncode) {
            fuzzString = encoder.getURLEncode(fuzz);
        } else {
            fuzzString = fuzz;
        }

        msg = fuzzableHttpMessage.fuzz(fuzzString);

        if (tokenValue != null) {
            // Replace token value - only supported in the body right now
            String replaced = msg.getRequestBody().toString();
            replaced = replaced.replace(encoder.getURLEncode(acsrfToken.getValue()),
                    encoder.getURLEncode(tokenValue));
            msg.setRequestBody(replaced);
        }
        // Correct the content length for the above changes
        msg.getRequestHeader().setContentLength(msg.getRequestBody().length());

        HttpSender httpSender = new HttpSender(connectionParam, true);
        if (followRedirects) {
            httpSender.setFollowRedirect(true);
        }
        httpSender.sendAndReceive(msg);

    } catch (HttpException e) {
        log.error(e.getMessage(), e);
        //FIXME use a cloned HttpMessage as it is needed in the Fuzzer panel, but should be shown an error message instead.
        msg = fuzzableHttpMessage.getHttpMessage().cloneRequest();
    } catch (IOException e) {
        log.error(e.getMessage(), e);
        //FIXME use a cloned HttpMessage as it is needed in the Fuzzer panel, but should be shown an error message instead.
        msg = fuzzableHttpMessage.getHttpMessage().cloneRequest();
    }
    for (FuzzerListener listener : listenerList) {
        listener.notifyFuzzProcessComplete(this);
    }
}

From source file:org.zaproxy.zap.extension.fuzz.impl.http.HttpFuzzProcess.java

@Override
public FuzzResult fuzz(String fuzz) {
    String tokenValue = null;//from w w  w  . ja  v a2 s  .co  m
    HttpFuzzResult fuzzResult = new HttpFuzzResult();

    if (this.acsrfToken != null) {
        // This currently just supports a single token in one page
        // To support wizards etc need to loop back up the messages for previous tokens
        try {
            HttpMessage tokenMsg = this.acsrfToken.getMsg().cloneAll();
            httpSender.sendAndReceive(tokenMsg);

            // If we've got a token value here then the AntiCSRF extension must have been registered
            tokenValue = extAntiCSRF.getTokenValue(tokenMsg, acsrfToken.getName());

            if (showTokenRequests) {
                List<HttpMessage> tokenRequests = new ArrayList<>();
                tokenRequests.add(tokenMsg);
                fuzzResult.setTokenRequestMessages(tokenRequests);
            }
        } catch (HttpException e) {
            logger.error(e.getMessage(), e);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
    }

    String fuzzString;
    if (urlEncode) {
        fuzzString = encoder.getURLEncode(fuzz);
    } else {
        fuzzString = fuzz;
    }

    HttpMessage msg;
    try {
        // Inject the payload
        msg = (HttpMessage) fuzzableHttpMessage.fuzz(fuzzString);
    } catch (Exception e) {
        msg = ((HttpMessage) fuzzableHttpMessage.getMessage()).cloneRequest();
        msg.setNote(fuzz);
        fuzzResult.setMessage(msg);

        fuzzResult.setState(State.ERROR);
        return fuzzResult;
    }

    msg.setNote(fuzz);

    if (tokenValue != null) {
        // Replace token value - only supported in the body right now
        String replaced = msg.getRequestBody().toString();
        replaced = replaced.replace(encoder.getURLEncode(acsrfToken.getValue()),
                encoder.getURLEncode(tokenValue));
        msg.setRequestBody(replaced);
    }
    // Correct the content length for the above changes
    msg.getRequestHeader().setContentLength(msg.getRequestBody().length());

    try {
        httpSender.sendAndReceive(msg);

        if (isFuzzStringReflected(msg, fuzz)) {
            fuzzResult.setState(State.REFLECTED);
        }
    } catch (HttpException e) {
        logger.error(e.getMessage(), e);
        fuzzResult.setState(State.ERROR);
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
        fuzzResult.setState(State.ERROR);
    }

    fuzzResult.setMessage(msg);

    return fuzzResult;
}

From source file:pl.umk.mat.zawodyweb.compiler.classes.LanguageOPSS.java

@Override
public TestOutput runTest(String path, TestInput input) {
    TestOutput result = new TestOutput(null);

    String loginUrl = "http://opss.assecobs.pl/?&login";
    String login = properties.getProperty("opss.login");
    String password = properties.getProperty("opss.password");

    HttpClient client = new HttpClient();

    HttpClientParams params = client.getParams();
    params.setParameter("http.useragent", "Opera/9.64 (Windows NT 6.0; U; pl) Presto/2.1.1");
    client.setParams(params);//  w ww  . j  av  a  2 s . c o m

    PostMethod logging = new PostMethod(loginUrl);
    NameValuePair[] dataLogging = { new NameValuePair("login_form_submit", "1"),
            new NameValuePair("login_form_login", login), new NameValuePair("login_form_pass", password) };
    logging.setRequestBody(dataLogging);
    try {
        client.executeMethod(logging);
    } catch (HttpException e) {
        result.setStatus(ResultsStatusEnum.UNDEF.getCode());
        result.setNotes(e.getMessage());
        result.setOutputText("HttpException");
        logging.releaseConnection();
        return result;
    } catch (IOException e) {
        result.setStatus(ResultsStatusEnum.UNDEF.getCode());
        result.setNotes(e.getMessage());
        result.setOutputText("IOException");
        logging.releaseConnection();
        return result;
    }
    logging.releaseConnection();
    PostMethod sendAnswer = new PostMethod("http://opss.assecobs.pl/?menu=comp&sub=send&comp=0");
    NameValuePair[] dataSendAnswer = { new NameValuePair("form_send_submit", "1"),
            new NameValuePair("form_send_comp", ""),
            new NameValuePair("form_send_problem", input.getInputText()),
            new NameValuePair("form_send_lang", properties.getProperty("CODEFILE_EXTENSION")),
            new NameValuePair("form_send_sourcetext", path),
            new NameValuePair("form_send_submittxt", "Wylij kod") };
    sendAnswer.setRequestBody(dataSendAnswer);
    InputStream status = null;
    try {
        client.executeMethod(sendAnswer);
        status = sendAnswer.getResponseBodyAsStream();
    } catch (HttpException e) {
        result.setStatus(ResultsStatusEnum.UNDEF.getCode());
        result.setNotes(e.getMessage());
        result.setOutputText("HttpException");
        sendAnswer.releaseConnection();
        return result;
    } catch (IOException e) {
        result.setStatus(ResultsStatusEnum.UNDEF.getCode());
        result.setNotes(e.getMessage());
        result.setOutputText("IOException");
        sendAnswer.releaseConnection();
        return result;
    }
    String submitId = null;
    BufferedReader br = null;
    try {
        br = new BufferedReader(new InputStreamReader(status, "iso-8859-2"));
    } catch (UnsupportedEncodingException e) {
    }
    String line;
    try {
        Pattern p = Pattern.compile("<a[^>]*href=[^>]*sub=stat[^>]*id=\\d*[^>]*>");
        line = br.readLine();
        Matcher m = p.matcher(line);
        while (!m.find() && line != null) {
            line = br.readLine();
            m = p.matcher(line);
        }
        Pattern p1 = Pattern.compile("id=\\d*");
        Matcher m1 = p1.matcher(m.group());
        m1.find();
        submitId = m1.group().split("=")[1];
    } catch (IOException e) {
        result.setStatus(ResultsStatusEnum.UNDEF.getCode());
        result.setNotes(e.getMessage());
        result.setOutputText("IOException");
        sendAnswer.releaseConnection();
        return result;
    }
    sendAnswer.releaseConnection();
    do {
        try {
            Thread.sleep(7000);
        } catch (InterruptedException e) {
            result.setStatus(ResultsStatusEnum.UNDEF.getCode());
            result.setNotes(e.getMessage());
            result.setOutputText("InterruptedException");
            return result;
        }
        GetMethod answer = new GetMethod("http://opss.assecobs.pl/?menu=comp&sub=stat&comp=0&id=" + submitId);
        InputStream answerStream = null;
        try {
            client.executeMethod(answer);
            answerStream = answer.getResponseBodyAsStream();
        } catch (HttpException e) {
            result.setStatus(ResultsStatusEnum.UNDEF.getCode());
            result.setNotes(e.getMessage());
            result.setOutputText("HttpException");
            answer.releaseConnection();
            return result;
        } catch (IOException e) {
            result.setStatus(ResultsStatusEnum.UNDEF.getCode());
            result.setNotes(e.getMessage());
            result.setOutputText("IOException");
            answer.releaseConnection();
            return result;
        }
        try {
            br = new BufferedReader(new InputStreamReader(answerStream, "iso-8859-2"));
        } catch (UnsupportedEncodingException e) {
        }
        String row = "";
        try {
            line = br.readLine();
            Pattern p2 = Pattern.compile("(<tr class=row0>)|(<tr class=stat_ac>)|(<tr class=stat_run>)");
            Matcher m2 = p2.matcher(line);
            while (!m2.find() && line != null) {
                line = br.readLine();
                m2 = p2.matcher(line);
            }
            row = "";
            for (int i = 0; i < 19; i++) {
                row += line;
                line = br.readLine();
            }
        } catch (IOException e) {
            result.setStatus(ResultsStatusEnum.UNDEF.getCode());
            result.setNotes(e.getMessage());
            result.setOutputText("IOException");
            sendAnswer.releaseConnection();
            return result;
        }
        String[] col = row.split("(<td>)|(</table>)");
        result.setPoints(0);
        if (col[6].matches(".*Program zaakceptowany.*")) {
            result.setStatus(ResultsStatusEnum.ACC.getCode());
            result.setPoints(input.getMaxPoints());
            result.setRuntime(10 * Integer.parseInt(col[8].replaceAll("\\.", "")));
            result.setMemUsed(Integer.parseInt(col[9].replaceAll("\\s", "")));
            break;
        } else if (col[6].matches(".*Bd kompilacji.*")) {
            result.setStatus(ResultsStatusEnum.CE.getCode());
            break;
        } else if (col[6].matches(".*Bd wykonania.*")) {
            result.setStatus(ResultsStatusEnum.RE.getCode());
            break;
        } else if (col[6].matches(".*Limit czasu przekroczony.*")) {
            result.setStatus(ResultsStatusEnum.TLE.getCode());
            break;
        } else if (col[6].matches(".*Limit pamici przekroczony.*")) {
            result.setStatus(ResultsStatusEnum.MLE.getCode());
            break;
        } else if (col[6].matches(".*Bdna odpowied.*")) {
            result.setStatus(ResultsStatusEnum.WA.getCode());
            break;
        } else if (col[6].matches(".*Niedozwolona funkcja.*")) {
            result.setStatus(ResultsStatusEnum.RV.getCode());
        } else if (col[6].matches(".*Uruchamianie.*")) {
        } else if (col[6].matches(".*Kompilacja.*")) {
        } else {
            result.setStatus(ResultsStatusEnum.UNKNOWN.getCode());
            result.setNotes("Unknown status: \"" + col[6] + "\"");
            break;
        }
        answer.releaseConnection();
    } while (true);
    GetMethod logout = new GetMethod("http://opss.assecobs.pl/?logoff");
    try {
        client.executeMethod(logout);
    } catch (HttpException e) {
        logout.releaseConnection();
    } catch (IOException e) {
        logout.releaseConnection();
    }
    logout.releaseConnection();
    return result;
}

From source file:poisondog.demo.HttpClientDemo.java

public static void main(String[] args) {
    HttpClientDemo demo = new HttpClientDemo();
    HttpClient client = demo.create("account", "pa5sw0rd");
    HttpMethod method = new GetMethod("http://www.apache.org/");

    try {/*  w ww  .  j av  a 2 s.co m*/
        int statusCode = client.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            System.err.println("Method failed: " + method.getStatusLine());
        }
        byte[] responseBody = method.getResponseBody();
        System.out.println(new String(responseBody));
    } catch (HttpException e) {
        System.err.println("Fatal protocol violation: " + e.getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("Fatal transport error: " + e.getMessage());
        e.printStackTrace();
    } finally {
        method.releaseConnection();
    }
}

From source file:sos.util.SOSHttpPost.java

private void sendFile(File inputFile, String outputFile) throws Exception {

    this.log_info("sending " + inputFile.getCanonicalPath() + " ...");

    int responseCode = -1;

    try {/*from  www .j a  v a 2  s.  com*/

        String suffix = null;
        String contentType = null;
        if (inputFile.getName().lastIndexOf('.') != -1) {

            suffix = inputFile.getName().substring(inputFile.getName().lastIndexOf('.') + 1);
            if (suffix.equals("htm") || suffix.equals("html"))
                contentType = "text/html";
            if (suffix.equals("xml"))
                contentType = "text/xml";
        }

        // encoding ermitteln
        if (contentType != null)
            if (contentType.equals("text/html") || contentType.equals("text/xml")) {

                BufferedReader br = new BufferedReader(new FileReader(inputFile));
                String buffer = "";
                String line = null;
                int c = 0;
                // die ersten 5 Zeilen nach einem encoding durchsuchen
                while ((line = br.readLine()) != null || ++c > 5)
                    buffer += line;
                Pattern p = Pattern.compile("encoding[\\s]*=[\\s]*['\"](.*?)['\"]", Pattern.CASE_INSENSITIVE);
                Matcher m = p.matcher(buffer);
                if (m.find())
                    contentType += "; charset=" + m.group(1);

                br.close();
            }

        if (contentType == null)
            contentType = "text/plain";

        PostMethod post = new PostMethod(this.targetURL);

        post.setRequestEntity(new InputStreamRequestEntity(new FileInputStream(inputFile), inputFile.length()));
        post.setRequestHeader("Content-Type", contentType);
        //post.setFollowRedirects(true);

        HttpClient httpClient = new HttpClient();

        responseCode = httpClient.executeMethod(post);

        // Response Code berprfen         
        // codes 200 - 399 gehen durch
        if (responseCode < 200 || responseCode >= 400)
            throw new HttpException(this.targetURL.toString() + " returns " + post.getStatusLine().toString());

        // response
        String writeMsg = "";
        if (outputFile != null)
            writeMsg = this.writeResponse(post.getResponseBodyAsStream(), outputFile);

        // abschlieende Meldung
        this.log_info(inputFile.getCanonicalPath() + " sent" + writeMsg);

    } catch (UnknownHostException uhx) {
        throw new Exception("UnknownHostException: " + uhx.getMessage());
    } catch (FileNotFoundException nfx) {
        throw new Exception("FileNotFoundException: " + nfx.getMessage());
    } catch (HttpException htx) {
        throw new Exception("HttpException: " + htx.getMessage());
    } catch (IOException iox) {
        throw new Exception("IOException: " + iox.getMessage());
    }
}

From source file:velo.adapters.GenericHttpClientAdapter.java

public int executeMethod(HttpMethod pm) throws AdapterException {
    try {//from   w w  w  .  j a v  a2s. c om
        return getHttpClient().executeMethod(pm);
    } catch (HttpException he) {
        throw new AdapterException(he.getMessage());
    } catch (IOException ioe) {
        throw new AdapterException(ioe.getMessage());
    }
}