public CacheItemHolder mapRow(ResultSet rs, Connection connection) throws SQLException { URI uri = URI.create(rs.getString("uri")); Vary vary = convertToVary(rs.getString("vary")); Blob blob = rs.getBlob("payload"); Payload payload = null; if (blob != null && !rs.wasNull()) { payload = new InputStreamPayload( new ResultSetInputStream(rs, connection, blob.getBinaryStream()), 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 CacheItem(rewriteResponse(response), cacheTime)); }
public State captureRemoteData(String urlStr) { HttpURLConnection connection = null; URL url = null; String suffix = null; try { url = new URL(urlStr); if (!validHost(url.getHost())) { return new BaseState(false, AppInfo.PREVENT_HOST); } connection = (HttpURLConnection) url.openConnection(); connection.setInstanceFollowRedirects(true); connection.setUseCaches(true); if (!validContentState(connection.getResponseCode())) { return new BaseState(false, AppInfo.CONNECTION_ERROR); } suffix = MIMEType.getSuffix(connection.getContentType()); if (!validFileType(suffix)) { return new BaseState(false, AppInfo.NOT_ALLOW_FILE_TYPE); } if (!validFileSize(connection.getContentLength())) { return new BaseState(false, AppInfo.MAX_SIZE); } String savePath = this.getPath(this.savePath, this.filename, suffix); String physicalPath = this.rootPath + savePath; ByteArrayOutputStream buffer = new ByteArrayOutputStream(); try { IOUtils.copy(connection.getInputStream(), buffer); } catch (IOException e) { e.printStackTrace(); } State state = StorageManager.saveFileByInputStream( new ByteArrayInputStream(buffer.toByteArray()), physicalPath); String filepath = OssUtils.putFile( new ByteArrayInputStream(buffer.toByteArray()), suffix.replaceFirst(".", "")) .getUrl(); buffer.close(); if (state.isSuccess()) { state.putInfo("url", filepath); state.putInfo("source", urlStr); } return state; } catch (Exception e) { return new BaseState(false, AppInfo.REMOTE_FAIL); } }