Search in sources :

Example 31 with ParseException

use of org.apache.http.ParseException in project robovm by robovm.

the class BasicLineParser method parseRequestLine.

/**
     * Parses a request line.
     *
     * @param buffer    a buffer holding the line to parse
     *
     * @return  the parsed request line
     *
     * @throws ParseException        in case of a parse error
     */
public RequestLine parseRequestLine(final CharArrayBuffer buffer, final ParserCursor cursor) throws ParseException {
    if (buffer == null) {
        throw new IllegalArgumentException("Char array buffer may not be null");
    }
    if (cursor == null) {
        throw new IllegalArgumentException("Parser cursor may not be null");
    }
    int indexFrom = cursor.getPos();
    int indexTo = cursor.getUpperBound();
    try {
        skipWhitespace(buffer, cursor);
        int i = cursor.getPos();
        int blank = buffer.indexOf(' ', i, indexTo);
        if (blank < 0) {
            throw new ParseException("Invalid request line: " + buffer.substring(indexFrom, indexTo));
        }
        String method = buffer.substringTrimmed(i, blank);
        cursor.updatePos(blank);
        skipWhitespace(buffer, cursor);
        i = cursor.getPos();
        blank = buffer.indexOf(' ', i, indexTo);
        if (blank < 0) {
            throw new ParseException("Invalid request line: " + buffer.substring(indexFrom, indexTo));
        }
        String uri = buffer.substringTrimmed(i, blank);
        cursor.updatePos(blank);
        ProtocolVersion ver = parseProtocolVersion(buffer, cursor);
        skipWhitespace(buffer, cursor);
        if (!cursor.atEnd()) {
            throw new ParseException("Invalid request line: " + buffer.substring(indexFrom, indexTo));
        }
        return createRequestLine(method, uri, ver);
    } catch (IndexOutOfBoundsException e) {
        throw new ParseException("Invalid request line: " + buffer.substring(indexFrom, indexTo));
    }
}
Also used : ParseException(org.apache.http.ParseException) ProtocolVersion(org.apache.http.ProtocolVersion)

Example 32 with ParseException

use of org.apache.http.ParseException in project asterixdb by apache.

the class ResultUtil method apiErrorHandler.

public static void apiErrorHandler(PrintWriter out, Exception e) {
    int errorCode = 99;
    if (e instanceof ParseException) {
        errorCode = 2;
    } else if (e instanceof AlgebricksException) {
        errorCode = 3;
    } else if (e instanceof HyracksDataException) {
        errorCode = 4;
    }
    ObjectNode errorResp = ResultUtil.getErrorResponse(errorCode, extractErrorMessage(e), extractErrorSummary(e), extractFullStackTrace(e));
    out.write(errorResp.toString());
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) ParseException(org.apache.http.ParseException) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Example 33 with ParseException

use of org.apache.http.ParseException in project platform_external_apache-http by android.

the class BasicLineParser method parseRequestLine.

/**
     * Parses a request line.
     *
     * @param buffer    a buffer holding the line to parse
     *
     * @return  the parsed request line
     *
     * @throws ParseException        in case of a parse error
     */
public RequestLine parseRequestLine(final CharArrayBuffer buffer, final ParserCursor cursor) throws ParseException {
    if (buffer == null) {
        throw new IllegalArgumentException("Char array buffer may not be null");
    }
    if (cursor == null) {
        throw new IllegalArgumentException("Parser cursor may not be null");
    }
    int indexFrom = cursor.getPos();
    int indexTo = cursor.getUpperBound();
    try {
        skipWhitespace(buffer, cursor);
        int i = cursor.getPos();
        int blank = buffer.indexOf(' ', i, indexTo);
        if (blank < 0) {
            throw new ParseException("Invalid request line: " + buffer.substring(indexFrom, indexTo));
        }
        String method = buffer.substringTrimmed(i, blank);
        cursor.updatePos(blank);
        skipWhitespace(buffer, cursor);
        i = cursor.getPos();
        blank = buffer.indexOf(' ', i, indexTo);
        if (blank < 0) {
            throw new ParseException("Invalid request line: " + buffer.substring(indexFrom, indexTo));
        }
        String uri = buffer.substringTrimmed(i, blank);
        cursor.updatePos(blank);
        ProtocolVersion ver = parseProtocolVersion(buffer, cursor);
        skipWhitespace(buffer, cursor);
        if (!cursor.atEnd()) {
            throw new ParseException("Invalid request line: " + buffer.substring(indexFrom, indexTo));
        }
        return createRequestLine(method, uri, ver);
    } catch (IndexOutOfBoundsException e) {
        throw new ParseException("Invalid request line: " + buffer.substring(indexFrom, indexTo));
    }
}
Also used : ParseException(org.apache.http.ParseException) ProtocolVersion(org.apache.http.ProtocolVersion)

Example 34 with ParseException

use of org.apache.http.ParseException in project lucene-solr by apache.

the class BasicHttpSolrClientTest method testQuery.

@Test
public void testQuery() throws Exception {
    DebugServlet.clear();
    try (HttpSolrClient client = getHttpSolrClient(jetty.getBaseUrl().toString() + "/debug/foo")) {
        SolrQuery q = new SolrQuery("foo");
        q.setParam("a", "ሴ");
        try {
            client.query(q, METHOD.GET);
        } catch (ParseException ignored) {
        }
        //default method
        assertEquals("get", DebugServlet.lastMethod);
        //agent
        assertEquals("Solr[" + HttpSolrClient.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
        //default wt
        assertEquals(1, DebugServlet.parameters.get(CommonParams.WT).length);
        assertEquals("javabin", DebugServlet.parameters.get(CommonParams.WT)[0]);
        //default version
        assertEquals(1, DebugServlet.parameters.get(CommonParams.VERSION).length);
        assertEquals(client.getParser().getVersion(), DebugServlet.parameters.get(CommonParams.VERSION)[0]);
        //agent
        assertEquals("Solr[" + HttpSolrClient.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
        //keepalive
        assertEquals("keep-alive", DebugServlet.headers.get("Connection"));
        //content-type
        assertEquals(null, DebugServlet.headers.get("Content-Type"));
        //param encoding
        assertEquals(1, DebugServlet.parameters.get("a").length);
        assertEquals("ሴ", DebugServlet.parameters.get("a")[0]);
        //POST
        DebugServlet.clear();
        try {
            client.query(q, METHOD.POST);
        } catch (ParseException ignored) {
        }
        assertEquals("post", DebugServlet.lastMethod);
        assertEquals("Solr[" + HttpSolrClient.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
        assertEquals(1, DebugServlet.parameters.get(CommonParams.WT).length);
        assertEquals("javabin", DebugServlet.parameters.get(CommonParams.WT)[0]);
        assertEquals(1, DebugServlet.parameters.get(CommonParams.VERSION).length);
        assertEquals(client.getParser().getVersion(), DebugServlet.parameters.get(CommonParams.VERSION)[0]);
        assertEquals(1, DebugServlet.parameters.get("a").length);
        assertEquals("ሴ", DebugServlet.parameters.get("a")[0]);
        assertEquals("Solr[" + HttpSolrClient.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
        assertEquals("keep-alive", DebugServlet.headers.get("Connection"));
        assertEquals("application/x-www-form-urlencoded; charset=UTF-8", DebugServlet.headers.get("Content-Type"));
        //PUT
        DebugServlet.clear();
        try {
            client.query(q, METHOD.PUT);
        } catch (ParseException ignored) {
        }
        assertEquals("put", DebugServlet.lastMethod);
        assertEquals("Solr[" + HttpSolrClient.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
        assertEquals(1, DebugServlet.parameters.get(CommonParams.WT).length);
        assertEquals("javabin", DebugServlet.parameters.get(CommonParams.WT)[0]);
        assertEquals(1, DebugServlet.parameters.get(CommonParams.VERSION).length);
        assertEquals(client.getParser().getVersion(), DebugServlet.parameters.get(CommonParams.VERSION)[0]);
        assertEquals(1, DebugServlet.parameters.get("a").length);
        assertEquals("ሴ", DebugServlet.parameters.get("a")[0]);
        assertEquals("Solr[" + HttpSolrClient.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
        assertEquals("keep-alive", DebugServlet.headers.get("Connection"));
        assertEquals("application/x-www-form-urlencoded; charset=UTF-8", DebugServlet.headers.get("Content-Type"));
        //XML/GET
        client.setParser(new XMLResponseParser());
        DebugServlet.clear();
        try {
            client.query(q, METHOD.GET);
        } catch (ParseException ignored) {
        }
        assertEquals("get", DebugServlet.lastMethod);
        assertEquals("Solr[" + HttpSolrClient.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
        assertEquals(1, DebugServlet.parameters.get(CommonParams.WT).length);
        assertEquals("xml", DebugServlet.parameters.get(CommonParams.WT)[0]);
        assertEquals(1, DebugServlet.parameters.get(CommonParams.VERSION).length);
        assertEquals(client.getParser().getVersion(), DebugServlet.parameters.get(CommonParams.VERSION)[0]);
        assertEquals(1, DebugServlet.parameters.get("a").length);
        assertEquals("ሴ", DebugServlet.parameters.get("a")[0]);
        assertEquals("Solr[" + HttpSolrClient.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
        assertEquals("keep-alive", DebugServlet.headers.get("Connection"));
        //XML/POST
        client.setParser(new XMLResponseParser());
        DebugServlet.clear();
        try {
            client.query(q, METHOD.POST);
        } catch (ParseException ignored) {
        }
        assertEquals("post", DebugServlet.lastMethod);
        assertEquals("Solr[" + HttpSolrClient.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
        assertEquals(1, DebugServlet.parameters.get(CommonParams.WT).length);
        assertEquals("xml", DebugServlet.parameters.get(CommonParams.WT)[0]);
        assertEquals(1, DebugServlet.parameters.get(CommonParams.VERSION).length);
        assertEquals(client.getParser().getVersion(), DebugServlet.parameters.get(CommonParams.VERSION)[0]);
        assertEquals(1, DebugServlet.parameters.get("a").length);
        assertEquals("ሴ", DebugServlet.parameters.get("a")[0]);
        assertEquals("Solr[" + HttpSolrClient.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
        assertEquals("keep-alive", DebugServlet.headers.get("Connection"));
        assertEquals("application/x-www-form-urlencoded; charset=UTF-8", DebugServlet.headers.get("Content-Type"));
        client.setParser(new XMLResponseParser());
        DebugServlet.clear();
        try {
            client.query(q, METHOD.PUT);
        } catch (ParseException ignored) {
        }
        assertEquals("put", DebugServlet.lastMethod);
        assertEquals("Solr[" + HttpSolrClient.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
        assertEquals(1, DebugServlet.parameters.get(CommonParams.WT).length);
        assertEquals("xml", DebugServlet.parameters.get(CommonParams.WT)[0]);
        assertEquals(1, DebugServlet.parameters.get(CommonParams.VERSION).length);
        assertEquals(client.getParser().getVersion(), DebugServlet.parameters.get(CommonParams.VERSION)[0]);
        assertEquals(1, DebugServlet.parameters.get("a").length);
        assertEquals("ሴ", DebugServlet.parameters.get("a")[0]);
        assertEquals("Solr[" + HttpSolrClient.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
        assertEquals("keep-alive", DebugServlet.headers.get("Connection"));
        assertEquals("application/x-www-form-urlencoded; charset=UTF-8", DebugServlet.headers.get("Content-Type"));
    }
}
Also used : ParseException(org.apache.http.ParseException) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Example 35 with ParseException

use of org.apache.http.ParseException in project lucene-solr by apache.

the class BasicHttpSolrClientTest method testQueryString.

@Test
public void testQueryString() throws Exception {
    final String clientUrl = jetty.getBaseUrl().toString() + "/debug/foo";
    try (HttpSolrClient client = getHttpSolrClient(clientUrl)) {
        // test without request query params
        DebugServlet.clear();
        client.setQueryParams(setOf("serverOnly"));
        UpdateRequest req = new UpdateRequest();
        setReqParamsOf(req, "serverOnly", "notServer");
        try {
            client.request(req);
        } catch (ParseException ignored) {
        }
        verifyServletState(client, req);
        // test without server query params
        DebugServlet.clear();
        client.setQueryParams(setOf());
        req = new UpdateRequest();
        req.setQueryParams(setOf("requestOnly"));
        setReqParamsOf(req, "requestOnly", "notRequest");
        try {
            client.request(req);
        } catch (ParseException ignored) {
        }
        verifyServletState(client, req);
        // test with both request and server query params
        DebugServlet.clear();
        req = new UpdateRequest();
        client.setQueryParams(setOf("serverOnly", "both"));
        req.setQueryParams(setOf("requestOnly", "both"));
        setReqParamsOf(req, "serverOnly", "requestOnly", "both", "neither");
        try {
            client.request(req);
        } catch (ParseException ignored) {
        }
        verifyServletState(client, req);
        // test with both request and server query params with single stream
        DebugServlet.clear();
        req = new UpdateRequest();
        req.add(new SolrInputDocument());
        client.setQueryParams(setOf("serverOnly", "both"));
        req.setQueryParams(setOf("requestOnly", "both"));
        setReqParamsOf(req, "serverOnly", "requestOnly", "both", "neither");
        try {
            client.request(req);
        } catch (ParseException ignored) {
        }
        // NOTE: single stream requests send all the params
        // as part of the query string.  So add "neither" to the request
        // so it passes the verification step.
        req.setQueryParams(setOf("requestOnly", "both", "neither"));
        verifyServletState(client, req);
    }
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) ParseException(org.apache.http.ParseException) Test(org.junit.Test)

Aggregations

ParseException (org.apache.http.ParseException)38 Header (org.apache.http.Header)14 ProtocolVersion (org.apache.http.ProtocolVersion)12 IOException (java.io.IOException)10 ProtocolException (org.apache.http.ProtocolException)9 HttpException (org.apache.http.HttpException)6 HttpParams (org.apache.http.params.HttpParams)6 Test (org.junit.Test)5 HttpEntity (org.apache.http.HttpEntity)4 Socket (java.net.Socket)3 UnknownHostException (java.net.UnknownHostException)3 ArrayList (java.util.ArrayList)3 LinkedList (java.util.LinkedList)3 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)3 SSLSocket (javax.net.ssl.SSLSocket)3 HeaderElement (org.apache.http.HeaderElement)3 HeaderIterator (org.apache.http.HeaderIterator)3 HttpConnection (org.apache.http.HttpConnection)3 HttpMessage (org.apache.http.HttpMessage)3 HttpResponse (org.apache.http.HttpResponse)3