implement some verbose classes for dealing with web requests

This commit is contained in:
mykola2312 2024-04-19 19:02:27 +03:00
parent 8d65968173
commit f223dd5110
5 changed files with 102 additions and 23 deletions

View file

@ -91,7 +91,7 @@ public class Main {
System.out.printf("fetchOne -> %d: %s\n", test.id, test.value);
WebRequest get = new WebRequest("https://example.com");
System.out.println(get.fetch());
System.out.println(get.fetch().body);
logger.info("mptv started");
}

View file

@ -1,5 +1,7 @@
package com.mykola2312.mptv.crawler;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.net.ssl.SSLContext;
@ -23,25 +25,31 @@ public class HttpClientFactory {
"DTLSv1.0",
};
public static HttpClient getHttpsClient() throws Exception {
public static HttpClient getHttpsClient() throws WebException {
if (client != null) {
return client;
}
SSLContext sslContext = SSLContexts
.custom()
.build();
sslContext.init(null, new TrustManager[] { new HttpsTrustManager() }, new SecureRandom());
try {
SSLContext sslContext = SSLContexts
.custom()
.build();
sslContext.init(null, new TrustManager[] { new HttpsTrustManager() }, new SecureRandom());
SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory(sslContext,
SUPPORTED_PROTOCOLS,
null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier()
);
client = HttpClients
.custom()
.setSSLSocketFactory(factory)
.build();
SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory(sslContext,
SUPPORTED_PROTOCOLS,
null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier()
);
client = HttpClients
.custom()
.setSSLSocketFactory(factory)
.build();
} catch (NoSuchAlgorithmException e) {
throw new WebException(e);
} catch (KeyManagementException e) {
throw new WebException(e);
}
return client;
}

View file

@ -0,0 +1,26 @@
package com.mykola2312.mptv.crawler;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
public class WebContent {
public int status;
public String body = null;
private static final Logger logger = Logger.getLogger(WebContent.class);
public WebContent(HttpResponse response) {
this.status = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
if (entity != null) {
try {
this.body = EntityUtils.toString(entity, "UTF-8");
} catch (Exception e) {
logger.warn(String.format("failed to get content for %s: %s", response.toString()));
logger.warn(e);
}
}
}
}

View file

@ -0,0 +1,44 @@
package com.mykola2312.mptv.crawler;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import org.apache.http.client.ClientProtocolException;
import org.apache.log4j.Logger;
// TODO: make more useful this stub exception
public class WebException extends RuntimeException {
public enum Type {
FAILED_TO_MAKE_CLIENT,
FETCH_FAILURE,
IO_ERROR,
};
public final Type type;
protected static final Logger logger = Logger.getLogger(WebException.class);
protected void log() {
logger.warn(String.format("%s: %s", type.name(), this.getCause().getMessage()));
}
public WebException(NoSuchAlgorithmException e) {
super(Type.FAILED_TO_MAKE_CLIENT.name(), e);
this.type = Type.FAILED_TO_MAKE_CLIENT;
}
public WebException(KeyManagementException e) {
super(Type.FAILED_TO_MAKE_CLIENT.name(), e);
this.type = Type.FAILED_TO_MAKE_CLIENT;
}
public WebException(ClientProtocolException e) {
super(Type.FETCH_FAILURE.name(), e);
this.type = Type.FETCH_FAILURE;
}
public WebException(IOException e) {
super(Type.IO_ERROR.name(), e);
this.type = Type.IO_ERROR;
}
}

View file

@ -1,10 +1,11 @@
package com.mykola2312.mptv.crawler;
import org.apache.http.HttpEntity;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
public class WebRequest {
private final HttpGet httpGet;
@ -13,16 +14,16 @@ public class WebRequest {
httpGet = new HttpGet(url);
}
public String fetch() {
public WebContent fetch() throws WebException {
try {
HttpClient client = HttpClientFactory.getHttpsClient();
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
return null;
return new WebContent(response);
} catch (ClientProtocolException e) {
throw new WebException(e);
} catch (IOException e) {
throw new WebException(e);
}
}
}