public static HTTPMethod valueOf(String method) { if (method == null || method.isEmpty()) { throw new IllegalArgumentException("method may not be null or empty"); } String uppercaseMethod = method.toUpperCase(Locale.ENGLISH); if (defaultMethods.containsKey(uppercaseMethod)) { return defaultMethods.get(uppercaseMethod); } return new HTTPMethod(uppercaseMethod); }
public void setPayload(Payload payload) { if (!method.canHavePayload()) { throw new IllegalStateException(String.format("Unable to add payload to a %s request", method)); } this.payload = payload; }
private void writeRequestLine(PrintStream writer, HTTPRequest request) { writer.print(String.format("%s %s HTTP/1.1\r\n", request.getMethod().toString(), request.getRequestURI().getPath())); } }
@Override public boolean equals(final Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } final HTTPRequest that = (HTTPRequest) o; if (challenge != null ? !challenge.equals(that.challenge) : that.challenge != null) { return false; } if (headers != null ? !headers.equals(that.headers) : that.headers != null) { return false; } if (method != null ? !method.equals(that.method) : that.method != null) { return false; } if (normalizedURI != null ? !normalizedURI.equals(that.normalizedURI) : that.normalizedURI != null) { return false; } return !(payload != null ? !payload.equals(that.payload) : that.payload != null); }
public Set<HTTPMethod> getAllow() { Optional<Header> header = getFirstHeader(HeaderConstants.ALLOW); if (header.isPresent()) { return header.get().getDirectives().stream().map(d -> HTTPMethod.valueOf(d.getName().toUpperCase(Locale.ENGLISH))).collect(Collectors.toSet()); } return Collections.emptySet(); }
@Override public int hashCode() { int result = method != null ? method.hashCode() : 0; result = 31 * result + (headers != null ? headers.hashCode() : 0); result = 31 * result + (challenge != null ? challenge.hashCode() : 0); result = 31 * result + (payload != null ? payload.hashCode() : 0); result = 31 * result + (normalizedURI != null ? normalizedURI.hashCode() : 0); return result; } }
@Deprecated public String name() { return getMethod(); }
public HTTPRequest withPayload(Payload payload) { if (!method.canHavePayload()) { throw new IllegalStateException(String.format("Unable to add payload to a %s request", method)); } return new HTTPRequest(requestURI, method, headers, challenge, Optional.ofNullable(payload)); }
private void doRequest(HTTPRequest request, HttpURLConnection connection) throws IOException { configureConnection(connection); connection.setRequestMethod(request.getMethod().getMethod()); Headers requestHeaders = request.getAllHeaders(); connection.addRequestProperty(HeaderConstants.USER_AGENT, getConfiguration().getUserAgent()); for (Header header : requestHeaders) { connection.addRequestProperty(header.getName(), header.getValue()); } connection.connect(); writeRequest(request, connection); }
public static MutableRequest fromHTTPRequest(HTTPRequest request) { MutableRequest mutableRequest = new MutableRequest( request.getRequestURI(), request.getMethod(), new MutableHeaders(request.getHeaders()), new MutableConditionals(request.getHeaders().getConditionals()) ); mutableRequest.setChallenge(request.getChallenge().orElse(null)); if (request.getMethod().canHavePayload()) { mutableRequest.setPayload(request.getPayload().orElse(null)); } return mutableRequest; }