public static Status valueOf(int statusCode) { Status status = STATUSES.get(statusCode); if (status != null) { return status; } return new Status(statusCode, "Unknown"); }
@Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } StatusLine that = (StatusLine) o; if (status != null ? !status.equals(that.status) : that.status != null) { return false; } if (version != that.version) { return false; } return true; }
public HttpURLConnectionStream(final HttpURLConnection connection, Status status) throws IOException { super(status.isClientError() || status.isServerError() ? connection.getErrorStream() : connection.getInputStream()); this.connection = connection; }
public static CacheItem parse(Properties object) { Optional<LocalDateTime> time = HeaderUtils.fromHttpDate(new Header("cache-time", object.getProperty("cache-time"))); Status status = Status.valueOf(NumberUtils.toInt(object.getProperty("status"), 200)); Headers headers = Headers.parse(object.getProperty("headers")); Optional<Payload> p = Optional.empty(); if (object.containsKey("file")) { p = Optional.of(new FilePayload(new File(object.getProperty("file")), headers.getContentType().get())); } return new DefaultCacheItem(new HTTPResponse(p, status, headers), time.get()); }
public StatusLine(HTTPVersion version, Status status, String message) { this.version = Objects.requireNonNull(version, "Version may not be null"); this.status = Objects.requireNonNull(status, "Status may not be null"); this.message = message == null ? status.getName() : message; }
@Override public int hashCode() { int result = version != null ? version.hashCode() : 0; result = 31 * result + (status != null ? status.hashCode() : 0); return result; }
public static HTTPResponse createResponse(final StatusLine line, final Headers responseHeaders, final Optional<InputStream> stream) { Optional<String> contentLengthHeader = responseHeaders.getFirstHeaderValue(HeaderConstants.CONTENT_LENGTH); MIMEType type = responseHeaders.getContentType().orElse(MIMEType.APPLICATION_OCTET_STREAM); Optional<Long> length = responseHeaders.getContentLength(); Optional<Payload> payload = stream. filter(is -> line.getStatus().isBodyContentAllowed()). map(is -> new InputStreamPayload(is, type, length.orElse(-1L))); return new HTTPResponse(payload, line, responseHeaders); } }
private HTTPResponse convertResponse(HttpURLConnection connection) throws IOException { Status status = Status.valueOf(connection.getResponseCode()); String message = connection.getResponseMessage(); Headers responseHeaders = getResponseHeaders(connection); return getResponseCreator().createResponse(new StatusLine(status, message), responseHeaders, wrapResponseStream(connection, status)); }
@Override public String toString() { return "HTTP/" + version.toString() + " " + status.getCode() + " " + message; } }
public CacheItemHolder mapRow(ResultSet rs) throws SQLException { URI uri = URI.create(rs.getString("uri")); Vary vary = convertToVary(rs.getString("vary")); Key key = Key.create(uri, vary); File file = manager.resolve(key); Payload payload = null; if (file.exists()) { payload = new FilePayload(file, MIMEType.valueOf(rs.getString("mimetype"))); } Status status = Status.valueOf(rs.getInt("status")); Headers headers = convertToHeaders(rs.getString("headers")); DateTime cacheTime = new DateTime(rs.getTimestamp("cachetime").getTime()); HTTPResponse response = new HTTPResponse(payload, status, headers); return new CacheItemHolder(uri, vary, new DefaultCacheItem(response, cacheTime)); } }
private static Map<Integer, Status> reflectAllStaticStatusFields() { Field[] fields = Status.class.getDeclaredFields(); List<Status> statuses = new ArrayList<>(); for (Field field : fields) { if (java.lang.reflect.Modifier.isStatic(field.getModifiers()) && field.getType() == Status.class) { boolean accessible = field.isAccessible(); field.setAccessible(true); try { Status status = (Status) field.get(null); if (status != null) { statuses.add(status); } field.setAccessible(accessible); } catch (IllegalAccessException e) { e.printStackTrace(); } } } Map<Integer, Status> map = new TreeMap<>(Comparator.naturalOrder()); for (Status s: statuses) { map.put(s.getCode(), s); } return Collections.unmodifiableMap(map); }
public Properties toProperties() { Properties object = new Properties(); object.setProperty("cache-time", HeaderUtils.toHttpDate("cache-time", item.getCachedTime()).getValue()); HTTPResponse response = item.getResponse(); object.setProperty("status", String.valueOf(response.getStatus().getCode())); if (response.hasPayload()) { FilePayload payload = (FilePayload) response.getPayload().get(); object.setProperty("file", payload.getFile().getAbsolutePath()); } object.setProperty("headers", response.getHeaders().toString()); return object; }
statement.setString(1, key.getURI().toString()); statement.setString(2, key.getVary().toJSON()); statement.setInt(3, response.getStatus().getCode()); statement.setString(4, response.getHeaders().toJSON()); if (response.hasPayload() && response.getPayload().isAvailable()) {