Codeverbesserungen, Dokumentation, Prefetch von Bildern in Originalgröße konfigurierbar gemacht.
git-svn-id: https://svn.libreccm.org/ccm/trunk@1837 8810af33-2d31-482b-a856-94f89814c4dfmaster
parent
16636b8d43
commit
3ffa2d01cc
|
|
@ -609,10 +609,14 @@ public final class CMSConfig extends AbstractConfig {
|
||||||
/////////////////////////////////////////////////
|
/////////////////////////////////////////////////
|
||||||
// ImageCache Parameter
|
// ImageCache Parameter
|
||||||
/////////////////////////////////////////////////
|
/////////////////////////////////////////////////
|
||||||
private final Parameter m_imageCache = new BooleanParameter(
|
private final Parameter m_imageCacheEnabled = new BooleanParameter(
|
||||||
"com.arsdigita.cms.image_cache.enable",
|
"com.arsdigita.cms.image_cache.enable",
|
||||||
Parameter.REQUIRED,
|
Parameter.REQUIRED,
|
||||||
true);
|
true);
|
||||||
|
private final Parameter m_imageCachePrefetchEnabled = new BooleanParameter(
|
||||||
|
"com.arsdigita.cms.image_cache.prefetch_enable",
|
||||||
|
Parameter.REQUIRED,
|
||||||
|
false);
|
||||||
private final Parameter m_imageCacheMaxSize = new IntegerParameter(
|
private final Parameter m_imageCacheMaxSize = new IntegerParameter(
|
||||||
"com.arsdigita.cms.image_cache.max_size",
|
"com.arsdigita.cms.image_cache.max_size",
|
||||||
Parameter.REQUIRED,
|
Parameter.REQUIRED,
|
||||||
|
|
@ -717,7 +721,8 @@ public final class CMSConfig extends AbstractConfig {
|
||||||
register(m_imageBrowserThumbnailMaxHeight);
|
register(m_imageBrowserThumbnailMaxHeight);
|
||||||
|
|
||||||
// ImageCache Parameter
|
// ImageCache Parameter
|
||||||
register(m_imageCache);
|
register(m_imageCacheEnabled);
|
||||||
|
register(m_imageCachePrefetchEnabled);
|
||||||
register(m_imageCacheMaxSize);
|
register(m_imageCacheMaxSize);
|
||||||
register(m_imageCacheMaxAge);
|
register(m_imageCacheMaxAge);
|
||||||
|
|
||||||
|
|
@ -1177,8 +1182,12 @@ public final class CMSConfig extends AbstractConfig {
|
||||||
return (Integer) get(m_imageBrowserThumbnailMaxHeight);
|
return (Integer) get(m_imageBrowserThumbnailMaxHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Boolean getImageCacheEnable() {
|
public Boolean getImageCacheEnabled() {
|
||||||
return (Boolean) get(m_imageCache);
|
return (Boolean) get(m_imageCacheEnabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Boolean getImageCachePrefetchEnabled() {
|
||||||
|
return (Boolean) get(m_imageCachePrefetchEnabled);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Integer getImageCacheMaxSize() {
|
public Integer getImageCacheMaxSize() {
|
||||||
|
|
|
||||||
|
|
@ -300,6 +300,11 @@ com.arsdigita.cms.image_cache.enable.purpose=Enable server-side coherent image c
|
||||||
com.arsdigita.cms.image_cache.enable.example=true|false
|
com.arsdigita.cms.image_cache.enable.example=true|false
|
||||||
com.arsdigita.cms.image_cache.enable.format=[boolean]
|
com.arsdigita.cms.image_cache.enable.format=[boolean]
|
||||||
|
|
||||||
|
com.arsdigita.cms.image_cache.prefetch_enable.title=Enable prefetching image cache
|
||||||
|
com.arsdigita.cms.image_cache.prefetch_enable.purpose=Enable image cache to store original sized images for resized images. This will need lots of RAM and you probably won't see a difference.
|
||||||
|
com.arsdigita.cms.image_cache.prefetch_enable.example=true|false
|
||||||
|
com.arsdigita.cms.image_cache.prefetch_enable.format=[boolean]
|
||||||
|
|
||||||
com.arsdigita.cms.image_cache.max_size.title=Image cache max elements
|
com.arsdigita.cms.image_cache.max_size.title=Image cache max elements
|
||||||
com.arsdigita.cms.image_cache.max_size.purpose=Max count of items in the image cache, keep this low to prevent filling your memory with unused images
|
com.arsdigita.cms.image_cache.max_size.purpose=Max count of items in the image cache, keep this low to prevent filling your memory with unused images
|
||||||
com.arsdigita.cms.image_cache.max_size.example=100
|
com.arsdigita.cms.image_cache.max_size.example=100
|
||||||
|
|
|
||||||
|
|
@ -11,15 +11,16 @@ import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
import org.imgscalr.Scalr;
|
import org.imgscalr.Scalr;
|
||||||
import org.imgscalr.Scalr.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* This is an in-memory copy of an {@link ImageAsset} to be stored in the image
|
||||||
|
* cache of {@link BaseImage}. Also, this class is able to create server-side
|
||||||
|
* resized versions of ImageAssets.
|
||||||
*
|
*
|
||||||
* @author Sören Bernstein (quasimodo) <sbernstein@zes.uni-bremen.de>
|
* @author Sören Bernstein (quasimodo) <sbernstein@zes.uni-bremen.de>
|
||||||
*/
|
*/
|
||||||
|
|
@ -34,11 +35,23 @@ public class CachedImage {
|
||||||
private BigDecimal height;
|
private BigDecimal height;
|
||||||
private static final Logger s_log = Logger.getLogger(CachedImage.class);
|
private static final Logger s_log = Logger.getLogger(CachedImage.class);
|
||||||
|
|
||||||
public CachedImage(ImageAsset imageAsset, int width, int height) {
|
/**
|
||||||
|
* Create a resized version of an ImageAsset for dispatching
|
||||||
|
*
|
||||||
|
* @param imageAsset the ImageAsset to save
|
||||||
|
* @param maxWidth the max width to resize the image to
|
||||||
|
* @param maxHeight the max height to resize the image to
|
||||||
|
*/
|
||||||
|
public CachedImage(ImageAsset imageAsset, int maxWidth, int maxHeight) {
|
||||||
this(imageAsset);
|
this(imageAsset);
|
||||||
this.resizeImage(width, height);
|
this.resizeImage(maxWidth, maxHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a original size version of an ImageAsset for dispatching
|
||||||
|
*
|
||||||
|
* @param imageAsset The ImageAsset to save
|
||||||
|
*/
|
||||||
public CachedImage(ImageAsset imageAsset) {
|
public CachedImage(ImageAsset imageAsset) {
|
||||||
|
|
||||||
this.hash = imageAsset.getOID().toString();
|
this.hash = imageAsset.getOID().toString();
|
||||||
|
|
@ -50,11 +63,20 @@ public class CachedImage {
|
||||||
this.height = imageAsset.getHeight();
|
this.height = imageAsset.getHeight();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a resized version of another CachedImage. This is a convienience
|
||||||
|
* constructor to handle the maxWidth and maxHeight param in a single
|
||||||
|
* String.
|
||||||
|
*
|
||||||
|
* @param cachedImage the cachedImage to resize
|
||||||
|
* @param resizeParam the resize paramter as
|
||||||
|
* "&maxWidth=<int>&maxHeight=<int>"
|
||||||
|
*/
|
||||||
public CachedImage(CachedImage cachedImage, String resizeParam) {
|
public CachedImage(CachedImage cachedImage, String resizeParam) {
|
||||||
this(cachedImage);
|
this(cachedImage);
|
||||||
|
|
||||||
int width = 0;
|
int maxWidth = 0;
|
||||||
int height = 0;
|
int maxHeight = 0;
|
||||||
|
|
||||||
String[] params = resizeParam.split("&");
|
String[] params = resizeParam.split("&");
|
||||||
for (int i = 0; i < params.length; i++) {
|
for (int i = 0; i < params.length; i++) {
|
||||||
|
|
@ -65,23 +87,35 @@ public class CachedImage {
|
||||||
String key = params[i].substring(0, params[i].indexOf("="));
|
String key = params[i].substring(0, params[i].indexOf("="));
|
||||||
String value = params[i].substring(params[i].indexOf("=") + 1);
|
String value = params[i].substring(params[i].indexOf("=") + 1);
|
||||||
|
|
||||||
if (key.equalsIgnoreCase("width")) {
|
if (key.equalsIgnoreCase("maxWidth")) {
|
||||||
width = Integer.parseInt(value);
|
maxWidth = Integer.parseInt(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (key.equalsIgnoreCase("height")) {
|
if (key.equalsIgnoreCase("maxHeight")) {
|
||||||
height = Integer.parseInt(value);
|
maxHeight = Integer.parseInt(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.resizeImage(width, height);
|
this.resizeImage(maxWidth, maxHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
public CachedImage(CachedImage cachedImage, int width, int height) {
|
/**
|
||||||
|
* Create a resized version aof another CacheImage.
|
||||||
|
*
|
||||||
|
* @param cachedImage the CachedImage to resize
|
||||||
|
* @param maxWidth max width of the image after resizing
|
||||||
|
* @param height max height of the image after resizing
|
||||||
|
*/
|
||||||
|
public CachedImage(CachedImage cachedImage, int maxWidth, int height) {
|
||||||
this(cachedImage);
|
this(cachedImage);
|
||||||
this.resizeImage(width, height);
|
this.resizeImage(maxWidth, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is just for internal use to set all the fields.
|
||||||
|
*
|
||||||
|
* @param cachedImage the CacheImage
|
||||||
|
*/
|
||||||
private CachedImage(CachedImage cachedImage) {
|
private CachedImage(CachedImage cachedImage) {
|
||||||
this.hash = cachedImage.hash;
|
this.hash = cachedImage.hash;
|
||||||
this.name = cachedImage.getName();
|
this.name = cachedImage.getName();
|
||||||
|
|
@ -92,61 +126,79 @@ public class CachedImage {
|
||||||
this.height = cachedImage.getHeight();
|
this.height = cachedImage.getHeight();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get filename
|
||||||
|
*
|
||||||
|
* @return the filename
|
||||||
|
*/
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return this.name;
|
return this.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get actual width of this instance
|
||||||
|
*
|
||||||
|
* @return image width
|
||||||
|
*/
|
||||||
public BigDecimal getWidth() {
|
public BigDecimal getWidth() {
|
||||||
return this.width;
|
return this.width;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get actual height of this instance
|
||||||
|
*
|
||||||
|
* @return image height
|
||||||
|
*/
|
||||||
public BigDecimal getHeight() {
|
public BigDecimal getHeight() {
|
||||||
return this.height;
|
return this.height;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get actual size of this instance
|
||||||
|
*
|
||||||
|
* @return image size
|
||||||
|
*/
|
||||||
public int getSize() {
|
public int getSize() {
|
||||||
return this.image.length;
|
return this.image.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get version (aka live of draft)
|
||||||
|
*/
|
||||||
public String getVersion() {
|
public String getVersion() {
|
||||||
return this.version;
|
return this.version;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get {@link MimeType}
|
||||||
|
*
|
||||||
|
* @return the {@MimeType}
|
||||||
|
*/
|
||||||
public MimeType getMimeType() {
|
public MimeType getMimeType() {
|
||||||
return this.mimetype;
|
return this.mimetype;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the image data
|
||||||
|
*
|
||||||
|
* @return the image data
|
||||||
|
*/
|
||||||
public byte[] getImage() {
|
public byte[] getImage() {
|
||||||
return this.image;
|
return this.image;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves the Blob content.
|
* Write the image to an OutputStream
|
||||||
*
|
*
|
||||||
* @return the Blob content
|
* @param os OutputStream to be written to
|
||||||
|
*
|
||||||
|
* @return number of bytes written
|
||||||
|
*
|
||||||
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
/*
|
|
||||||
protected byte[] getContent() {
|
|
||||||
byte[] content = null;
|
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
||||||
|
|
||||||
try {
|
|
||||||
ImageIO.write(image, "JPEG", out);
|
|
||||||
content = out.toByteArray();
|
|
||||||
} catch (IOException ioEx) {
|
|
||||||
s_log.warn("Could not write byte array", ioEx);
|
|
||||||
} catch (IllegalArgumentException illEx) {
|
|
||||||
s_log.warn("image is not initialized", illEx);
|
|
||||||
} finally {
|
|
||||||
return content;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
public long writeBytes(OutputStream os) throws IOException {
|
public long writeBytes(OutputStream os) throws IOException {
|
||||||
byte[] bytes = this.getImage();
|
os.write(this.getImage());
|
||||||
os.write(bytes);
|
return (long) (this.getSize());
|
||||||
|
|
||||||
return (long) (bytes.length);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -154,8 +206,7 @@ public class CachedImage {
|
||||||
*
|
*
|
||||||
* @param file The file on the server to write to.
|
* @param file The file on the server to write to.
|
||||||
*/
|
*/
|
||||||
public void writeToFile(File file)
|
public void writeToFile(File file) throws IOException {
|
||||||
throws IOException {
|
|
||||||
FileOutputStream fs = new FileOutputStream(file);
|
FileOutputStream fs = new FileOutputStream(file);
|
||||||
try {
|
try {
|
||||||
fs.write(this.getImage());
|
fs.write(this.getImage());
|
||||||
|
|
@ -167,6 +218,16 @@ public class CachedImage {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to proportional resize an image into the defined boundaries.
|
||||||
|
* Either width or height can be 0.
|
||||||
|
* If height is 0, the image will be fit to width.
|
||||||
|
* If width is 0, the image will be fit to height.
|
||||||
|
* If both paramters are 0, this method will not do anything.
|
||||||
|
*
|
||||||
|
* @param width max width of the image after resizing
|
||||||
|
* @param height max height of the image after resizing
|
||||||
|
*/
|
||||||
private void resizeImage(int width, int height) {
|
private void resizeImage(int width, int height) {
|
||||||
|
|
||||||
// No valid resizing imformation
|
// No valid resizing imformation
|
||||||
|
|
@ -218,4 +279,18 @@ public class CachedImage {
|
||||||
return this.hash.hashCode();
|
return this.hash.hashCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (obj == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (getClass() != obj.getClass()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
final CachedImage other = (CachedImage) obj;
|
||||||
|
if ((this.hash == null) ? (other.hash != null) : !this.hash.equals(other.hash)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,8 +22,8 @@ import com.arsdigita.bebop.parameters.BigDecimalParameter;
|
||||||
import com.arsdigita.caching.CacheTable;
|
import com.arsdigita.caching.CacheTable;
|
||||||
import com.arsdigita.cms.Asset;
|
import com.arsdigita.cms.Asset;
|
||||||
import com.arsdigita.cms.CMS;
|
import com.arsdigita.cms.CMS;
|
||||||
import com.arsdigita.cms.ImageAsset;
|
|
||||||
import com.arsdigita.cms.CachedImage;
|
import com.arsdigita.cms.CachedImage;
|
||||||
|
import com.arsdigita.cms.ImageAsset;
|
||||||
import com.arsdigita.dispatcher.DispatcherHelper;
|
import com.arsdigita.dispatcher.DispatcherHelper;
|
||||||
import com.arsdigita.dispatcher.RequestContext;
|
import com.arsdigita.dispatcher.RequestContext;
|
||||||
import com.arsdigita.domain.DataObjectNotFoundException;
|
import com.arsdigita.domain.DataObjectNotFoundException;
|
||||||
|
|
@ -31,21 +31,24 @@ import com.arsdigita.domain.DomainObjectFactory;
|
||||||
import com.arsdigita.mimetypes.MimeType;
|
import com.arsdigita.mimetypes.MimeType;
|
||||||
import com.arsdigita.persistence.OID;
|
import com.arsdigita.persistence.OID;
|
||||||
import com.arsdigita.toolbox.ui.OIDParameter;
|
import com.arsdigita.toolbox.ui.OIDParameter;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A resource handler which streams out a blob from the database.
|
* A resource handler which streams out a blob from the database.
|
||||||
|
* This class can use a special image cache to speed up image dispatching. Also,
|
||||||
|
* during dispatch this class will create server-side resized images depending
|
||||||
|
* on the URL parameter. Resizing is done by ImageScalr. The image cahce can be
|
||||||
|
* activated and configured by com.arsdigita.cms.image_cache.* parameters.
|
||||||
*
|
*
|
||||||
* @author Stanislav Freidin (sfreidin@arsdigita.com)
|
* @author Stanislav Freidin (sfreidin@arsdigita.com)
|
||||||
* @author Michael Pih (pihman@arsdigita.com)
|
* @author Michael Pih (pihman@arsdigita.com)
|
||||||
|
* @author Sören Bernstein <sbernstein@zes.uni-bremen.de>
|
||||||
* @version $Revision: #20 $ $DateTime: 2004/08/17 23:15:09 $
|
* @version $Revision: #20 $ $DateTime: 2004/08/17 23:15:09 $
|
||||||
* @version $Id: BaseImage.java 1571 2007-04-20 15:57:54Z apevec $
|
* @version $Id: BaseImage.java 1571 2007-04-20 15:57:54Z apevec $
|
||||||
*/
|
*/
|
||||||
|
|
@ -62,12 +65,13 @@ public class BaseImage extends ResourceHandlerImpl {
|
||||||
private static CacheTable s_imageCache = null;
|
private static CacheTable s_imageCache = null;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
if (CMS.getConfig().getImageCacheEnable()) {
|
if (CMS.getConfig().getImageCacheEnabled()) {
|
||||||
s_imageCache = new CacheTable("BaseImageCache",
|
s_imageCache = new CacheTable("BaseImageCache",
|
||||||
CMS.getConfig().getImageCacheMaxAge(),
|
CMS.getConfig().getImageCacheMaxAge(),
|
||||||
CMS.getConfig().getImageCacheMaxSize());
|
CMS.getConfig().getImageCacheMaxSize());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
private final bool IMAGE_CACHE_PREFETCH = CMS.getConfig().getImageCachePrefetchEnabled();
|
||||||
private static final Logger s_log = Logger.getLogger(BaseImage.class);
|
private static final Logger s_log = Logger.getLogger(BaseImage.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -142,9 +146,9 @@ public class BaseImage extends ResourceHandlerImpl {
|
||||||
/**
|
/**
|
||||||
* Streams an image from the database.
|
* Streams an image from the database.
|
||||||
*
|
*
|
||||||
* @param request The servlet request object
|
* @param request The servlet request object
|
||||||
* @param response the servlet response object
|
* @param response the servlet response object
|
||||||
* @param actx The request context
|
* @param actx The request context
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void dispatch(HttpServletRequest request,
|
public void dispatch(HttpServletRequest request,
|
||||||
|
|
@ -158,8 +162,8 @@ public class BaseImage extends ResourceHandlerImpl {
|
||||||
String resizeParam = "";
|
String resizeParam = "";
|
||||||
|
|
||||||
// Get URL parameters
|
// Get URL parameters
|
||||||
String widthParam = request.getParameter("width");
|
String maxWidthParam = request.getParameter("maxWidth");
|
||||||
String heightParam = request.getParameter("height");
|
String maxHeightParam = request.getParameter("maxHeight");
|
||||||
|
|
||||||
// Need the OID, but can work with imageId
|
// Need the OID, but can work with imageId
|
||||||
try {
|
try {
|
||||||
|
|
@ -185,114 +189,56 @@ public class BaseImage extends ResourceHandlerImpl {
|
||||||
// Finally, we have a valid OID
|
// Finally, we have a valid OID
|
||||||
|
|
||||||
// Process URL parameter
|
// Process URL parameter
|
||||||
if (widthParam != null && heightParam != null) {
|
if (maxWidthParam != null && maxHeightParam != null) {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
// Set width
|
// Set width if supplied by URL parameter
|
||||||
if (!widthParam.isEmpty() && widthParam.matches("^[0-9]*$")) {
|
if (!maxWidthParam.isEmpty() && maxWidthParam.matches("^[0-9]*$")) {
|
||||||
resizeParam += "&width=" + widthParam;
|
resizeParam += "&maxWidth=" + maxWidthParam;
|
||||||
}
|
}
|
||||||
} catch (NumberFormatException numberEx) {
|
} catch (NumberFormatException numberEx) {
|
||||||
s_log.warn("width parameter invalid " + widthParam);
|
s_log.warn("maxWidth parameter invalid " + maxWidthParam);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
// Set height
|
// Set height if supplied by URL parameter
|
||||||
if (!heightParam.isEmpty() && heightParam.matches("^[0-9]*$")) {
|
if (!maxHeightParam.isEmpty() && maxHeightParam.matches("^[0-9]*$")) {
|
||||||
resizeParam += "&height=" + heightParam;
|
resizeParam += "&maxHeight=" + maxHeightParam;
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (NumberFormatException numberEx) {
|
} catch (NumberFormatException numberEx) {
|
||||||
s_log.warn("height parameter invalid " + heightParam);
|
s_log.warn("maxHeight parameter invalid " + maxHeightParam);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Now, we have all information we need to proceed
|
// Now, we have all information we need to proceed
|
||||||
|
|
||||||
if (!resizeParam.isEmpty()) {
|
// Get the image
|
||||||
|
cachedImage = this.getImage(response, oid, resizeParam);
|
||||||
// Try to get the CachedImage with the OID from the imageCache
|
if (cachedImage == null) {
|
||||||
cachedImage = (CachedImage) s_imageCache.get(oid.toString() + resizeParam);
|
// ok, something is really weird now. Can't find image with this oid. Bailing out.
|
||||||
|
return;
|
||||||
// If cachedImage is still null, the resized version of this oid is
|
|
||||||
// not in the cache. So, we try to find the original version to
|
|
||||||
// avoid unnesseccary database access
|
|
||||||
if (cachedImage == null) {
|
|
||||||
|
|
||||||
// Get the original version
|
|
||||||
cachedImage = (CachedImage) s_imageCache.get(oid.toString());
|
|
||||||
|
|
||||||
// If cachedImage is still null, it is not in the imageCache
|
|
||||||
if (cachedImage == null) {
|
|
||||||
|
|
||||||
// Get it from the database
|
|
||||||
cachedImage = this.getImageAssetFromDB(response, oid);
|
|
||||||
|
|
||||||
// If cachedImage is still null, we can't find the oid in the DB either
|
|
||||||
// There is something broken. Bail out.
|
|
||||||
if (cachedImage == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Put the CachedImage into the imageCache
|
|
||||||
s_imageCache.put(oid.toString(), cachedImage);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a resized version of the cachedImage
|
|
||||||
cachedImage = new CachedImage(cachedImage, resizeParam);
|
|
||||||
|
|
||||||
// Put the CacheImageAsset into the imageCache
|
|
||||||
s_imageCache.put(oid.toString() + resizeParam, cachedImage);
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
// Try to get the CachedImage with the OID from the imageCache
|
|
||||||
cachedImage = (CachedImage) (s_imageCache.get(oid.toString()));
|
|
||||||
|
|
||||||
// If cachedImage is still null, it is not in the imageCache
|
|
||||||
if (cachedImage == null) {
|
|
||||||
|
|
||||||
// Get it from the database
|
|
||||||
cachedImage = this.getImageAssetFromDB(response, oid);
|
|
||||||
|
|
||||||
// If cachedImage is still null, we can't find the oid in the DB either
|
|
||||||
// There is something broken. Bail out.
|
|
||||||
if (cachedImage == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Put the CacheImageAsset into the imageCache
|
|
||||||
s_imageCache.put(oid.toString(), cachedImage);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setHeaders(response, cachedImage);
|
setHeaders(response, cachedImage);
|
||||||
send(response, cachedImage);
|
send(response, cachedImage);
|
||||||
}
|
}
|
||||||
|
|
||||||
private CachedImage getCachedImage(HttpServletResponse response, OID oid, String resizeParam) throws IOException {
|
private CachedImage getImage(HttpServletResponse response, OID oid, String resizeParam) throws IOException {
|
||||||
|
|
||||||
CachedImage cachedImage = null;
|
CachedImage cachedImage = null;
|
||||||
|
|
||||||
|
// Test for cache
|
||||||
if (s_imageCache != null) {
|
if (s_imageCache != null) {
|
||||||
cachedImage = (CachedImage) s_imageCache.get(oid.toString() + resizeParam);
|
|
||||||
|
|
||||||
if (cachedImage == null) {
|
// Image cache is enabled, try to fetch images from cache
|
||||||
|
cachedImage = getImageFromCache(response, oid, resizeParam);
|
||||||
|
|
||||||
if (!resizeParam.isEmpty()) {
|
|
||||||
cachedImage = (CachedImage) s_imageCache.get(oid.toString());
|
|
||||||
|
|
||||||
// Create a resized version of the cachedImage
|
|
||||||
cachedImage = new CachedImage(cachedImage, resizeParam);
|
|
||||||
|
|
||||||
// Put the CacheImageAsset into the imageCache
|
|
||||||
s_imageCache.put(oid.toString() + resizeParam, cachedImage);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
cachedImage = getImageAssetFromDB(response, oid);
|
|
||||||
|
|
||||||
|
// Image cache is disabled
|
||||||
|
// Get the original image from db
|
||||||
|
cachedImage = getImageFromDB(response, oid);
|
||||||
if (cachedImage != null && !resizeParam.isEmpty()) {
|
if (cachedImage != null && !resizeParam.isEmpty()) {
|
||||||
cachedImage = new CachedImage(cachedImage, resizeParam);
|
cachedImage = new CachedImage(cachedImage, resizeParam);
|
||||||
}
|
}
|
||||||
|
|
@ -301,7 +247,61 @@ public class BaseImage extends ResourceHandlerImpl {
|
||||||
return cachedImage;
|
return cachedImage;
|
||||||
}
|
}
|
||||||
|
|
||||||
private CachedImage getImageAssetFromDB(HttpServletResponse response, OID oid) throws IOException {
|
/**
|
||||||
|
* Fetches the {@link CachedImage} from the image cache. If tge object
|
||||||
|
* could not be found in the cache, this method falls back to
|
||||||
|
* {@link #getImageFromDB(javax.servlet.http.HttpServletResponse, com.arsdigita.persistence.OID)}.
|
||||||
|
* This method will also store the CachedImage in the image cache for future
|
||||||
|
* use.
|
||||||
|
*
|
||||||
|
* @param response The HttpServletResponse
|
||||||
|
* @param oid the {@link OID} of the wanted object
|
||||||
|
* @param resizeParam the resize paramters of the wanted object
|
||||||
|
* @return the wanted {@link CachedImage} in the correct size or null, if the object could not be found
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
private CachedImage getImageFromCache(HttpServletResponse response, OID oid, String resizeParam) throws IOException {
|
||||||
|
CachedImage cachedImage;
|
||||||
|
|
||||||
|
cachedImage = (CachedImage) s_imageCache.get(oid.toString() + resizeParam);
|
||||||
|
|
||||||
|
// If we coundn't find the specific version
|
||||||
|
if (cachedImage == null) {
|
||||||
|
|
||||||
|
// If we were looking for a resized version
|
||||||
|
if (!resizeParam.isEmpty()) {
|
||||||
|
|
||||||
|
// try to find the original version in the cache by recursion
|
||||||
|
cachedImage = this.getImageFromCache(response, oid, "");
|
||||||
|
|
||||||
|
if (cachedImage != null) {
|
||||||
|
cachedImage = new CachedImage(cachedImage, resizeParam);
|
||||||
|
s_imageCache.put(oid.toString() + resizeParam, cachedImage);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
|
||||||
|
// look for the original version in the database
|
||||||
|
cachedImage = getImageFromDB(response, oid);
|
||||||
|
|
||||||
|
// If we found the image, put it into the image cache
|
||||||
|
if (cachedImage != null && IMAGE_CACHE_PREFETCH) {
|
||||||
|
s_imageCache.put(oid.toString(), cachedImage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return cachedImage;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetches the {@link ImageAsset} with the supplied {@link OID} from the database
|
||||||
|
* and converts it to a {@CachedImage}.
|
||||||
|
*
|
||||||
|
* @param response the HttpServletResponse
|
||||||
|
* @param oid the {@link OID} to the ImageAsset
|
||||||
|
* @return the ImageAsset with the oid as CachedImage or null, if not found
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
private CachedImage getImageFromDB(HttpServletResponse response, OID oid) throws IOException {
|
||||||
|
|
||||||
ImageAsset imageAsset = null;
|
ImageAsset imageAsset = null;
|
||||||
|
|
||||||
|
|
@ -311,6 +311,7 @@ public class BaseImage extends ResourceHandlerImpl {
|
||||||
try {
|
try {
|
||||||
Asset a = (Asset) DomainObjectFactory.newInstance(oid);
|
Asset a = (Asset) DomainObjectFactory.newInstance(oid);
|
||||||
|
|
||||||
|
// Make sure we have an ImageAsset
|
||||||
if (a instanceof ImageAsset) {
|
if (a instanceof ImageAsset) {
|
||||||
imageAsset = (ImageAsset) a;
|
imageAsset = (ImageAsset) a;
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -319,8 +320,7 @@ public class BaseImage extends ResourceHandlerImpl {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (DataObjectNotFoundException nfe) {
|
} catch (DataObjectNotFoundException nfe) {
|
||||||
response.sendError(HttpServletResponse.SC_NOT_FOUND,
|
response.sendError(HttpServletResponse.SC_NOT_FOUND, "no ImageAsset with oid " + oid);
|
||||||
"no ImageAsset with oid " + oid);
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -83,7 +83,7 @@ public class ImageBrowser extends Table {
|
||||||
* Construct a new ImageBrowser with default mode.
|
* Construct a new ImageBrowser with default mode.
|
||||||
*
|
*
|
||||||
* @param builder the {@link ImageBrowserModelBuilder} that will supply this
|
* @param builder the {@link ImageBrowserModelBuilder} that will supply this
|
||||||
* component with its {@link ImageBrowserModel} during each request
|
* component with its {@link ImageBrowserModel} during each request
|
||||||
*/
|
*/
|
||||||
public ImageBrowser(ImageBrowserModelBuilder b) {
|
public ImageBrowser(ImageBrowserModelBuilder b) {
|
||||||
|
|
||||||
|
|
@ -94,14 +94,14 @@ public class ImageBrowser extends Table {
|
||||||
* Construct a new ImageBrowser with requested mode.
|
* Construct a new ImageBrowser with requested mode.
|
||||||
*
|
*
|
||||||
* @param builder the {@link ImageBrowserModelBuilder} that will supply this
|
* @param builder the {@link ImageBrowserModelBuilder} that will supply this
|
||||||
* component with its {@link ImageBrowserModel} during each request
|
* component with its {@link ImageBrowserModel} during each request
|
||||||
* @param mode the component mode (see {@link ImageComponent})
|
* @param mode the component mode (see {@link ImageComponent})
|
||||||
*/
|
*/
|
||||||
public ImageBrowser(ImageBrowserModelBuilder b, int mode) {
|
public ImageBrowser(ImageBrowserModelBuilder b, int mode) {
|
||||||
super(new BuilderAdapter(b), HEADERS);
|
super(new BuilderAdapter(b), HEADERS);
|
||||||
m_mode = mode;
|
m_mode = mode;
|
||||||
setThumbnailSize(CMS.getConfig().getImageBrowserThumbnailMaxWidth(),
|
setThumbnailSize(CMS.getConfig().getImageBrowserThumbnailMaxWidth(),
|
||||||
CMS.getConfig().getImageBrowserThumbnailMaxHeight());
|
CMS.getConfig().getImageBrowserThumbnailMaxHeight());
|
||||||
m_builder = b;
|
m_builder = b;
|
||||||
|
|
||||||
getHeader().setDefaultRenderer(new DefaultTableCellRenderer(false));
|
getHeader().setDefaultRenderer(new DefaultTableCellRenderer(false));
|
||||||
|
|
@ -123,7 +123,7 @@ public class ImageBrowser extends Table {
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getNumColumns() {
|
public int getNumColumns() {
|
||||||
return m_numColumns;
|
return m_numColumns;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -151,6 +151,7 @@ public class ImageBrowser extends Table {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param state The current page state
|
* @param state The current page state
|
||||||
|
*
|
||||||
* @return the {@link ImageBrowserModel} used in the current request
|
* @return the {@link ImageBrowserModel} used in the current request
|
||||||
*/
|
*/
|
||||||
public ImageBrowserModel getImageBrowserModel(PageState state) {
|
public ImageBrowserModel getImageBrowserModel(PageState state) {
|
||||||
|
|
@ -189,7 +190,8 @@ public class ImageBrowser extends Table {
|
||||||
ImageAsset a = (ImageAsset) value;
|
ImageAsset a = (ImageAsset) value;
|
||||||
|
|
||||||
String url = Service.getImageURL(a);
|
String url = Service.getImageURL(a);
|
||||||
String resizeParam = "&width=" + new Double(m_thumbSize.getWidth()).intValue() + "&height=" + new Double(m_thumbSize.getHeight()).intValue();
|
// Sets url paramter to resize the images server-side
|
||||||
|
String resizeParam = "&maxWidth=" + new Double(m_thumbSize.getWidth()).intValue() + "&maxHeight=" + new Double(m_thumbSize.getHeight()).intValue();
|
||||||
|
|
||||||
Image img = new Image(URL.getDispatcherPath() + url + resizeParam, a.getName());
|
Image img = new Image(URL.getDispatcherPath() + url + resizeParam, a.getName());
|
||||||
img.setBorder("0");
|
img.setBorder("0");
|
||||||
|
|
@ -293,7 +295,7 @@ public class ImageBrowser extends Table {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getColumnCount() {
|
public int getColumnCount() {
|
||||||
return ((ImageBrowser)m_model).getNumColumns();
|
return ((ImageBrowser) m_model).getNumColumns();
|
||||||
// return ImageBrowser.s_numColumns;
|
// return ImageBrowser.s_numColumns;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -343,7 +345,7 @@ public class ImageBrowser extends Table {
|
||||||
return m.getMimeType();
|
return m.getMimeType();
|
||||||
|
|
||||||
case ImageBrowser.LINK:
|
case ImageBrowser.LINK:
|
||||||
return "select";
|
return "select";
|
||||||
|
|
||||||
case ImageBrowser.DELETE:
|
case ImageBrowser.DELETE:
|
||||||
return "delete";
|
return "delete";
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue