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);
}
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);
}
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;
}
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());
}
}
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());
}
}
Aggregations