private InputStream open() throws Exception { URLConnection conn; InputStream in; if (client != null) { TaggedData tag = client.connectTagged(url); conn = tag.getConnection(); in = tag.getInputStream(); } else { conn = url.openConnection(); conn.connect(); in = conn.getInputStream(); } lastModified = conn.getLastModified(); int length = conn.getContentLength(); if (length != -1) { size = length; } return in; }
@Override public String toString() { return "TaggedData [tag=" + getTag() + ", code=" + getResponseCode() + ", modified=" + new Date(getModified()) + ", url=" + getUrl() + ", state=" + getState() + (message == null ? "" : ", msg=" + message) + "]"; }
private String cleanHtml(CharSequence sb) { sb = HTML_TAGS_P.matcher(sb) .replaceAll(""); sb = NEWLINES_P.matcher(sb) .replaceAll("\n"); StringBuffer x = new StringBuffer(); Matcher m = ENTITIES_P.matcher(sb); while (m.find()) { if (m.group("nr") != null) { char c = (char) Integer.parseInt(m.group("nr")); m.appendReplacement(x, ""); x.append(c); } else { m.appendReplacement(x, entity(m.group("name"))); } } m.appendTail(x); return x.toString(); }
public HttpRequestException(TaggedData tag) { super(tag.getUrl() + ":" + tag.getResponseCode() + ":" + tag.getTag()); this.responseCode = tag.getResponseCode(); }
private State fetch(List<MavenBackingRepository> mbrs, String remotePath, File file) throws Exception { State error = State.NOT_FOUND; for (MavenBackingRepository mbr : mbrs) { TaggedData fetch = mbr.fetch(remotePath, file); switch (fetch.getState()) { case NOT_FOUND : break; case OTHER : error = State.OTHER; logger.error("Fetching artifact gives error {} : {} {}", remotePath, fetch.getResponseCode(), fetch); break; case UNMODIFIED : case UPDATED : return fetch.getState(); } } return error; }
File sourceFile = new File(url.toURI()); if (!sourceFile.isFile()) return new TaggedData(url.toURI(), 404, null); return new TaggedData(url.toURI(), 304, info.file); return new TaggedData(url.toURI(), 200, info.file); if (in.getState() == State.UPDATED) { info.update(in.getInputStream(), in.getTag(), in.getModified()); } else if (in.getState() == State.UNMODIFIED) info.jsonFile.setLastModified(System.currentTimeMillis()); return new TaggedData(request.url.toURI(), HTTP_NOT_MODIFIED, info.file); return new TaggedData(url.toURI(), 404, request.useCacheFile); if (in.isOk()) { info.update(in.getInputStream(), in.getTag(), in.getModified());
public Object send(final HttpRequest<?> request) throws Exception { if (isOffline() || request.isCache()) { return doCached(request); } else { TaggedData in = send0(request); if (request.download == TaggedData.class) return in; switch (in.getState()) { case NOT_FOUND : return null; case OTHER : in.throwIt(); return null; case UNMODIFIED : case UPDATED : default : break; } return convert(request.download, in.getInputStream()); } }
RevisionMetadata getMetadata(Revision revision) throws Exception { File metafile = IO.getFile(local, revision.metadata(id)); RevisionMetadata metadata = revisions.get(revision); TaggedData tag = fetch(revision.metadata(), metafile); if (tag.getState() == State.NOT_FOUND || tag.getState() == State.OTHER) { if (metadata == null) { metadata = new RevisionMetadata(); revisions.put(revision, metadata); } return metadata; } if (metadata == null || tag.getState() == State.UPDATED) { metadata = MetadataParser.parseRevisionMetadata(metafile); revisions.put(revision, metadata); } return metadata; }
private TaggedData connectTagged(URL url, String tag, String user, String pass) throws Exception { TaggedData result; HttpURLConnection connection = (HttpURLConnection) url.openConnection(); try { if (disableSslVerify) HttpsUtil.disableServerVerification(connection); } catch (GeneralSecurityException e) { if (reporter != null) reporter.error("Error attempting to disable SSL server certificate verification: %s", e); throw new IOException("Error attempting to disable SSL server certificate verification."); } // Add the authorization string using HTTP Basic Auth if (user != null && pass != null) { String authString = user + ":" + pass; String encoded = Base64.encodeBase64(authString.getBytes(UTF_8)); connection.setRequestProperty(HEADER_AUTHORIZATION, PREFIX_BASIC_AUTH + encoded); } // Add the ETag if (tag != null) connection.setRequestProperty(HEADER_IF_NONE_MATCH, tag); connection.connect(); int responseCode = connection.getResponseCode(); if (responseCode == RESPONSE_NOT_MODIFIED) result = null; else { String responseTag = connection.getHeaderField(HEADER_ETAG); result = new TaggedData(connection, connection.getInputStream()); } return result; }
switch (tag.getState()) { case NOT_FOUND : metadata = new ProgramMetadata(); throw new HttpRequestException((HttpURLConnection) tag.getConnection());
@Override public void close() throws IOException { IO.close(getInputStream()); } }
Object doCached(final HttpRequest<?> request) throws Exception, IOException { TaggedData tag = doCached0(request); if (request.download == TaggedData.class) return tag; if (request.download == State.class) return tag.getState(); switch (tag.getState()) { case NOT_FOUND : return null; case OTHER : throw new HttpRequestException(tag); case UNMODIFIED : case UPDATED : default : return convert(request.download, request.useCacheFile == null ? tag.getFile() : request.useCacheFile, tag); } }
public boolean isOk() { return getResponseCode() / 100 == 2; }
public TaggedData(URLConnection con, InputStream in, File file) throws Exception { this.con = con; this.responseCode = con instanceof HttpURLConnection ? ((HttpURLConnection) con).getResponseCode() : (in != null ? 200 : -1); this.in = in == null && con != null && (responseCode / 100 == 2) ? con.getInputStream() : in; this.file = file; this.etag = con.getHeaderField("ETag"); this.url = con.getURL() .toURI(); this.message = getMessage(con); }
private String getMessage(URLConnection con) { try { if (con == null || !(con instanceof HttpURLConnection)) return null; HttpURLConnection h = (HttpURLConnection) con; if (h.getResponseCode() / 100 < 4) return null; StringBuffer sb = new StringBuffer(); try { InputStream in = con.getInputStream(); if (in != null) sb.append(IO.collect(in)); } catch (Exception e) { // ignore } try { InputStream errorStream = h.getErrorStream(); if (errorStream != null) sb.append(IO.collect(errorStream)); } catch (Exception e) { // ignore } return cleanHtml(sb); } catch (Exception e) { return null; } }
File sourceFile = new File(url.toURI()); if (!sourceFile.isFile()) return new TaggedData(url.toURI(), 404, null); return new TaggedData(url.toURI(), 304, info.file); return new TaggedData(url.toURI(), 200, info.file); if (in.getState() == State.UPDATED) { info.update(in.getInputStream(), in.getTag(), in.getModified()); } else if (in.getState() == State.UNMODIFIED) info.jsonFile.setLastModified(System.currentTimeMillis()); return new TaggedData(request.url.toURI(), HTTP_NOT_MODIFIED, info.file); return new TaggedData(url.toURI(), 404, request.useCacheFile); if (in.isOk()) { info.update(in.getInputStream(), in.getTag(), in.getModified());
public Object send(final HttpRequest<?> request) throws Exception { if (isOffline() || request.isCache()) { return doCached(request); } else { TaggedData in = send0(request); if (request.download == TaggedData.class) return in; switch (in.getState()) { case NOT_FOUND : return null; case OTHER : in.throwIt(); return null; case UNMODIFIED : case UPDATED : default : break; } return convert(request.download, in.getInputStream()); } }
private State fetch(List<MavenBackingRepository> mbrs, String remotePath, File file) throws Exception { State error = State.NOT_FOUND; for (MavenBackingRepository mbr : mbrs) { TaggedData fetch = mbr.fetch(remotePath, file); switch (fetch.getState()) { case NOT_FOUND : break; case OTHER : error = State.OTHER; logger.error("Fetching artifact gives error {} : {} {}", remotePath, fetch.getResponseCode(), fetch); break; case UNMODIFIED : case UPDATED : return fetch.getState(); } } return error; }
public HttpRequestException(TaggedData tag) { super(tag.getUrl() + ":" + tag.getResponseCode() + ":" + tag.getTag()); this.responseCode = tag.getResponseCode(); }