Merge remote-tracking branch 'origin/master'

This commit is contained in:
Benjamin Gamard 2017-12-11 10:17:19 +01:00
commit 9bb73bb35b
4 changed files with 34 additions and 86 deletions

View File

@ -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.
* Build an Expires HTTP header.
*
* @param url URL to load
* @return Contents of the resource
* @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);
}
}

View File

@ -9,6 +9,7 @@ import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.IOException;
@ -40,6 +41,17 @@ public class ImageUtil {
iwp.setCompressionQuality(1.f);
imageOutputStream = ImageIO.createImageOutputStream(outputStream);
writer.setOutput(imageOutputStream);
if (image.getColorModel().hasAlpha()) {
// Strip alpha channel
BufferedImage noAlphaImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics graphics = noAlphaImage.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, image.getWidth(), image.getHeight());
graphics.drawImage(image, 0, 0, null);
image = noAlphaImage;
}
IIOImage iioImage = new IIOImage(image, null, null);
writer.write(null, iioImage, iwp);
} finally {

View File

@ -24,6 +24,7 @@ import com.sismics.rest.exception.ForbiddenClientException;
import com.sismics.rest.exception.ServerException;
import com.sismics.rest.util.JsonUtil;
import com.sismics.rest.util.ValidationUtil;
import com.sismics.util.HttpUtil;
import com.sismics.util.context.ThreadLocalContext;
import com.sismics.util.mime.MimeType;
import com.sismics.util.mime.MimeTypeUtil;
@ -34,6 +35,7 @@ import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObjectBuilder;
import javax.ws.rs.*;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
@ -46,8 +48,6 @@ import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@ -597,9 +597,10 @@ public class FileResource extends BaseResource {
}
return Response.ok(stream)
.header("Content-Disposition", "inline; filename=" + file.getFullName("data"))
.header("Content-Type", mimeType)
.header("Expires", new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z").format(new Date().getTime() + 3600000 * 24))
.header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=" + file.getFullName("data"))
.header(HttpHeaders.CONTENT_TYPE, mimeType)
.header(HttpHeaders.CACHE_CONTROL, "private")
.header(HttpHeaders.EXPIRES, HttpUtil.buildExpiresHeader(3_600_000L * 24L * 365L))
.build();
}

View File

@ -12,12 +12,14 @@ import com.sismics.rest.exception.ForbiddenClientException;
import com.sismics.rest.exception.ServerException;
import com.sismics.rest.util.JsonUtil;
import com.sismics.rest.util.ValidationUtil;
import com.sismics.util.HttpUtil;
import com.sismics.util.css.Selector;
import org.glassfish.jersey.media.multipart.FormDataBodyPart;
import org.glassfish.jersey.media.multipart.FormDataParam;
import javax.json.*;
import javax.ws.rs.*;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.IOException;
@ -26,8 +28,6 @@ import java.io.OutputStream;
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
/**
@ -235,8 +235,9 @@ public class ThemeResource extends BaseResource {
}
}
})
.header("Content-Type", "image/*")
.header("Expires", new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z").format(new Date().getTime() + 3600000 * 24 * 15))
.header(HttpHeaders.CONTENT_TYPE, "image/*")
.header(HttpHeaders.CACHE_CONTROL, "public")
.header(HttpHeaders.EXPIRES, HttpUtil.buildExpiresHeader(3_600_000L * 24L * 15L))
.build();
}