Skip to content

Commit

Permalink
move to separate classes
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Dec 21, 2024
1 parent 5c2149f commit 6ba0bbd
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 60 deletions.
65 changes: 5 additions & 60 deletions core/src/main/java/lucee/commons/net/HTTPUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -41,7 +39,6 @@

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.message.BasicHeader;

import lucee.commons.digest.Hash;
import lucee.commons.io.IOUtil;
Expand All @@ -52,6 +49,10 @@
import lucee.commons.lang.StringUtil;
import lucee.commons.lang.mimetype.ContentType;
import lucee.commons.lang.mimetype.MimeType;
import lucee.commons.net.header.HeadersCollection;
import lucee.commons.net.header.HeadersHTTPResponse;
import lucee.commons.net.header.HeadersHttpResponseApache;
import lucee.commons.net.header.HeadersHttpURLConnection;
import lucee.commons.net.http.HTTPEngine;
import lucee.commons.net.http.HTTPResponse;
import lucee.commons.net.http.httpclient.HTTPEngine4Impl;
Expand Down Expand Up @@ -698,62 +699,6 @@ public static boolean isSecure(URL url) {
return StringUtil.indexOfIgnoreCase(url.getProtocol(), "https") != -1;
}

public static interface HeadersCollection {
public Header[] getHeaders(String name);
}

public static class HeadersHttpURLConnection implements HeadersCollection {

private HttpURLConnection conn;

public HeadersHttpURLConnection(HttpURLConnection conn) {
this.conn = conn;
}

@Override
public Header[] getHeaders(String name) {
String val = conn.getHeaderField(name);
if (!StringUtil.isEmpty(val, true)) {
return new Header[] { new BasicHeader(name, val) };
}
return null;
}
}

public static class HeadersHTTPResponse implements HeadersCollection {

private HTTPResponse response;

public HeadersHTTPResponse(HTTPResponse response) {
this.response = response;
}

@Override
public Header[] getHeaders(String name) {
List<Header> list = new ArrayList<>();
for (lucee.commons.net.http.Header header: response.getAllHeaders()) {
if (name.equals(header.getName())) {
list.add(new BasicHeader(header.getName(), header.getValue()));
}
}
return list.toArray(new Header[list.size()]);
}
}

public static class HeadersHttpResponse implements HeadersCollection {

private HttpResponse response;

public HeadersHttpResponse(HttpResponse response) {
this.response = response;
}

@Override
public Header[] getHeaders(String name) {
return response.getHeaders(name);
}
}

public static void validateDownload(URL url, HttpURLConnection conn, Resource res, boolean deleteFileWhenInvalid, Exception cause) throws IOException {
_validateDownload(url, new HeadersHttpURLConnection(conn), res, deleteFileWhenInvalid, cause);
}
Expand All @@ -763,7 +708,7 @@ public static void validateDownload(URL url, HTTPResponse rsp, Resource res, boo
}

public static void validateDownload(URL url, HttpResponse rsp, Resource res, boolean deleteFileWhenInvalid, Exception cause) throws IOException {
_validateDownload(url, new HeadersHttpResponse(rsp), res, deleteFileWhenInvalid, cause);
_validateDownload(url, new HeadersHttpResponseApache(rsp), res, deleteFileWhenInvalid, cause);
}

private static void _validateDownload(URL url, HeadersCollection headersCollection, Resource res, boolean deleteFileWhenInvalid, Exception cause) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package lucee.commons.net.header;

import org.apache.http.Header;

public interface HeadersCollection {
public Header[] getHeaders(String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package lucee.commons.net.header;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.message.BasicHeader;

import lucee.commons.net.http.HTTPResponse;

public class HeadersHTTPResponse implements HeadersCollection {

private HTTPResponse response;

public HeadersHTTPResponse(HTTPResponse response) {
this.response = response;
}

@Override
public Header[] getHeaders(String name) {
List<Header> list = new ArrayList<>();
for (lucee.commons.net.http.Header header: response.getAllHeaders()) {
if (name.equals(header.getName())) {
list.add(new BasicHeader(header.getName(), header.getValue()));
}
}
return list.toArray(new Header[list.size()]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package lucee.commons.net.header;

import org.apache.http.Header;
import org.apache.http.HttpResponse;

public class HeadersHttpResponseApache implements HeadersCollection {

private HttpResponse response;

public HeadersHttpResponseApache(HttpResponse response) {
this.response = response;
}

@Override
public Header[] getHeaders(String name) {
return response.getHeaders(name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package lucee.commons.net.header;

import java.net.HttpURLConnection;

import org.apache.http.Header;
import org.apache.http.message.BasicHeader;

import lucee.commons.lang.StringUtil;

public class HeadersHttpURLConnection implements HeadersCollection {

private HttpURLConnection conn;

public HeadersHttpURLConnection(HttpURLConnection conn) {
this.conn = conn;
}

@Override
public Header[] getHeaders(String name) {
String val = conn.getHeaderField(name);
if (!StringUtil.isEmpty(val, true)) {
return new Header[] { new BasicHeader(name, val) };
}
return null;
}
}

0 comments on commit 6ba0bbd

Please sign in to comment.