mirror of
https://github.com/sismics/docs.git
synced 2025-12-14 10:16:21 +00:00
Closes #172: smart images caching
This commit is contained in:
@@ -1,14 +1,8 @@
|
||||
package com.sismics.util;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* HTTP request utilities.
|
||||
@@ -17,77 +11,17 @@ import java.net.URLConnection;
|
||||
*/
|
||||
public class HttpUtil {
|
||||
/**
|
||||
* Logger.
|
||||
* Format of the expires header.
|
||||
*/
|
||||
private static final Logger log = LoggerFactory.getLogger(HttpUtil.class);
|
||||
private static final SimpleDateFormat EXPIRES_FORMAT = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
|
||||
|
||||
/**
|
||||
* Loads the content of an URL into a string.
|
||||
*
|
||||
* @param url URL to load
|
||||
* @return Contents of the resource
|
||||
* Build an Expires HTTP header.
|
||||
*
|
||||
* @param futureTime Expire interval
|
||||
* @return Formatted header value
|
||||
*/
|
||||
public static String readUrlIntoString(URL url) {
|
||||
URLConnection connection;
|
||||
BufferedReader in = null;
|
||||
try {
|
||||
connection = url.openConnection();
|
||||
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String inputLine;
|
||||
while ((inputLine = in.readLine()) != null) {
|
||||
sb.append(inputLine);
|
||||
}
|
||||
return sb.toString();
|
||||
} catch (IOException e) {
|
||||
if (log.isErrorEnabled()) {
|
||||
log.error("Error reading URL", e);
|
||||
}
|
||||
return null;
|
||||
} finally {
|
||||
if (in != null) {
|
||||
try {
|
||||
in.close();
|
||||
} catch (Exception e) {
|
||||
// NOP
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String postUrl(URL url, String data) throws IOException {
|
||||
OutputStreamWriter wr = null;
|
||||
BufferedReader rd = null;
|
||||
try {
|
||||
URLConnection conn = url.openConnection();
|
||||
conn.setDoOutput(true);
|
||||
wr = new OutputStreamWriter(conn.getOutputStream());
|
||||
wr.write(data);
|
||||
wr.flush();
|
||||
|
||||
// Get the response
|
||||
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String line = null;
|
||||
while ((line = rd.readLine()) != null) {
|
||||
sb.append(line).append("\n");
|
||||
}
|
||||
return sb.toString();
|
||||
} finally {
|
||||
if (wr != null) {
|
||||
try {
|
||||
wr.close();
|
||||
} catch (IOException e) {
|
||||
// NOP
|
||||
}
|
||||
}
|
||||
if (rd != null) {
|
||||
try {
|
||||
rd.close();
|
||||
} catch (IOException e) {
|
||||
// NOP
|
||||
}
|
||||
}
|
||||
}
|
||||
public static String buildExpiresHeader(long futureTime) {
|
||||
return EXPIRES_FORMAT.format(new Date().getTime() + futureTime);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user