Search in sources :

Example 6 with ParseException

use of org.apache.hc.core5.http.ParseException in project httpcomponents-core by apache.

the class BasicLineParser method parseStatusLine.

@Override
public StatusLine parseStatusLine(final CharArrayBuffer buffer) throws ParseException {
    Args.notNull(buffer, "Char array buffer");
    final ParserCursor cursor = new ParserCursor(0, buffer.length());
    this.tokenizer.skipWhiteSpace(buffer, cursor);
    final ProtocolVersion ver = parseProtocolVersion(buffer, cursor);
    this.tokenizer.skipWhiteSpace(buffer, cursor);
    final String s = this.tokenizer.parseToken(buffer, cursor, BLANKS);
    for (int i = 0; i < s.length(); i++) {
        if (!Character.isDigit(s.charAt(i))) {
            throw new ParseException("Status line contains invalid status code", buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
        }
    }
    final int statusCode;
    try {
        statusCode = Integer.parseInt(s);
    } catch (final NumberFormatException e) {
        throw new ParseException("Status line contains invalid status code", buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
    }
    final String text = buffer.substringTrimmed(cursor.getPos(), cursor.getUpperBound());
    return new StatusLine(ver, statusCode, text);
}
Also used : ParseException(org.apache.hc.core5.http.ParseException) ProtocolVersion(org.apache.hc.core5.http.ProtocolVersion)

Example 7 with ParseException

use of org.apache.hc.core5.http.ParseException in project httpcomponents-core by apache.

the class BasicLineParser method parseProtocolVersion.

ProtocolVersion parseProtocolVersion(final CharArrayBuffer buffer, final ParserCursor cursor) throws ParseException {
    final String protoname = this.protocol.getProtocol();
    final int protolength = protoname.length();
    this.tokenizer.skipWhiteSpace(buffer, cursor);
    final int pos = cursor.getPos();
    // long enough for "HTTP/1.1"?
    if (pos + protolength + 4 > cursor.getUpperBound()) {
        throw new ParseException("Invalid protocol version", buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
    }
    // check the protocol name and slash
    boolean ok = true;
    for (int i = 0; ok && (i < protolength); i++) {
        ok = buffer.charAt(pos + i) == protoname.charAt(i);
    }
    if (ok) {
        ok = buffer.charAt(pos + protolength) == '/';
    }
    if (!ok) {
        throw new ParseException("Invalid protocol version", buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
    }
    cursor.updatePos(pos + protolength + 1);
    final String token1 = this.tokenizer.parseToken(buffer, cursor, FULL_STOP);
    final int major;
    try {
        major = Integer.parseInt(token1);
    } catch (final NumberFormatException e) {
        throw new ParseException("Invalid protocol major version number", buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
    }
    if (cursor.atEnd()) {
        throw new ParseException("Invalid protocol version", buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
    }
    cursor.updatePos(cursor.getPos() + 1);
    final String token2 = this.tokenizer.parseToken(buffer, cursor, BLANKS);
    final int minor;
    try {
        minor = Integer.parseInt(token2);
    } catch (final NumberFormatException e) {
        throw new ParseException("Invalid protocol minor version number", buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
    }
    return HttpVersion.get(major, minor);
}
Also used : ParseException(org.apache.hc.core5.http.ParseException)

Example 8 with ParseException

use of org.apache.hc.core5.http.ParseException in project para by Erudika.

the class TwitterAuthFilter method stepOne.

private boolean stepOne(HttpServletResponse response, String redirectURI, String[] keys) throws IOException {
    String callback = Utils.urlEncode(redirectURI);
    Map<String, String[]> params = new HashMap<>();
    params.put("oauth_callback", new String[] { callback });
    HttpPost tokenPost = new HttpPost(FLOW_URL1);
    tokenPost.setHeader(HttpHeaders.AUTHORIZATION, OAuth1HmacSigner.sign("POST", FLOW_URL1, params, keys[0], keys[1], null, null));
    tokenPost.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
    try (CloseableHttpResponse resp1 = httpclient.execute(tokenPost)) {
        if (resp1.getCode() == HttpServletResponse.SC_OK) {
            String decoded = EntityUtils.toString(resp1.getEntity());
            EntityUtils.consumeQuietly(resp1.getEntity());
            for (String pair : decoded.split("&")) {
                if (pair.startsWith("oauth_token")) {
                    response.sendRedirect(FLOW_URL2 + pair);
                    return true;
                } else {
                    logger.info("Authentication request failed, token not found in response - " + decoded);
                }
            }
        } else {
            logger.info("Authentication request failed with status '" + resp1.getReasonPhrase() + "' and empty response body.");
        }
    } catch (ParseException e) {
        logger.error(null, e);
    }
    return false;
}
Also used : HttpPost(org.apache.hc.client5.http.classic.methods.HttpPost) HashMap(java.util.HashMap) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) ParseException(org.apache.hc.core5.http.ParseException)

Example 9 with ParseException

use of org.apache.hc.core5.http.ParseException in project spotify-web-api-java by spotify-web-api-java.

the class GetCurrentUsersProfileExample method getCurrentUsersProfile_Sync.

public static void getCurrentUsersProfile_Sync() {
    try {
        final User user = getCurrentUsersProfileRequest.execute();
        System.out.println("Display name: " + user.getDisplayName());
    } catch (IOException | SpotifyWebApiException | ParseException e) {
        System.out.println("Error: " + e.getMessage());
    }
}
Also used : User(se.michaelthelin.spotify.model_objects.specification.User) IOException(java.io.IOException) ParseException(org.apache.hc.core5.http.ParseException) SpotifyWebApiException(se.michaelthelin.spotify.exceptions.SpotifyWebApiException)

Example 10 with ParseException

use of org.apache.hc.core5.http.ParseException in project spotify-web-api-java by spotify-web-api-java.

the class GetPlaylistExample method getPlaylist_Sync.

public static void getPlaylist_Sync() {
    try {
        final Playlist playlist = getPlaylistRequest.execute();
        System.out.println("Name: " + playlist.getName());
    } catch (IOException | SpotifyWebApiException | ParseException e) {
        System.out.println("Error: " + e.getMessage());
    }
}
Also used : Playlist(se.michaelthelin.spotify.model_objects.specification.Playlist) IOException(java.io.IOException) ParseException(org.apache.hc.core5.http.ParseException) SpotifyWebApiException(se.michaelthelin.spotify.exceptions.SpotifyWebApiException)

Aggregations

ParseException (org.apache.hc.core5.http.ParseException)62 IOException (java.io.IOException)54 SpotifyWebApiException (se.michaelthelin.spotify.exceptions.SpotifyWebApiException)33 CloseableHttpResponse (org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)24 HttpPost (org.apache.hc.client5.http.classic.methods.HttpPost)13 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)12 CloseableHttpClient (org.apache.hc.client5.http.impl.classic.CloseableHttpClient)11 HttpEntity (org.apache.hc.core5.http.HttpEntity)10 HashMap (java.util.HashMap)8 Map (java.util.Map)6 StringEntity (org.apache.hc.core5.http.io.entity.StringEntity)6 BasicNameValuePair (org.apache.hc.core5.http.message.BasicNameValuePair)6 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)5 ArrayList (java.util.ArrayList)5 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)5 Matcher (java.util.regex.Matcher)4 UrlEncodedFormEntity (org.apache.hc.client5.http.entity.UrlEncodedFormEntity)4 AuthorizationCodeCredentials (se.michaelthelin.spotify.model_objects.credentials.AuthorizationCodeCredentials)4 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)3 List (java.util.List)3