Example usage for org.apache.wicket.util.time Time millis

List of usage examples for org.apache.wicket.util.time Time millis

Introduction

In this page you can find the example usage for org.apache.wicket.util.time Time millis.

Prototype

public static Time millis(final long time) 

Source Link

Document

Retrieves a Time instance based on the given milliseconds.

Usage

From source file:eu.uqasar.web.components.resources.UserPictureResource.java

License:Apache License

@Override
protected byte[] getImageData(Attributes attributes) {
    Long attrUserId = getUserId(attributes);
    int attrMaxSize = getMaxDimension(attributes);
    try {//from w  w  w .  j a  va 2  s  .co  m
        BufferedImage im;
        BufferedImage thumb;
        Path userPicturePath = UserProfilePictureUploadHelper.getUserPicturePath(attrUserId);
        if (attrUserId != null && userPicturePath != null) {
            im = ImageIO.read(userPicturePath.toFile());
            setLastModifiedTime(Time.millis(userPicturePath.toFile().lastModified()));
        } else {
            setLastModifiedTime(Time.millis(75675677));
            im = ImageIO.read(defaultProfilePictureRef.getCacheableResourceStream().getInputStream());
        }
        if (im.getWidth() > attrMaxSize || im.getHeight() > attrMaxSize) {
            // TODO add caching, currently every request scales the image!!
            thumb = Scalr.resize(im, Scalr.Method.ULTRA_QUALITY, Scalr.Mode.AUTOMATIC, attrMaxSize);
        } else {
            thumb = im;
        }
        byte[] bytes;
        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
            ImageIO.write(thumb, "PNG", baos);
            bytes = baos.toByteArray();
        }
        return bytes;
    } catch (IOException | ResourceStreamNotFoundException ignored) {
    }
    return null;
}

From source file:fiftyfive.wicket.util.LoggingUtils.java

License:Apache License

/**
 * Returns the amount of time the current request has taken up until
 * this point./*w w w.j a v  a  2s.co m*/
 */
public static Duration getRequestDuration() {
    Time start = Time.millis(RequestCycle.get().getStartTime());
    return Duration.elapsed(start);
}

From source file:guru.mmp.application.web.template.resources.ErrorImageResourceStream.java

License:Apache License

/**
 * Returns the last modified time for the resource stream.
 *
 * @return the last modified time for the resource stream
 *//*  w w  w.  ja v  a2  s.  c om*/
@Override
public Time lastModifiedTime() {
    return Time.millis(0);
}

From source file:net.dontdrinkandroot.extensions.wicket.resource.AbstractFileThumbnailResource.java

License:Apache License

@Override
protected ResourceResponse newResourceResponse(final Attributes attributes) {
    final File file = this.resolveFile(attributes);

    final Integer width = this.resolveWidth(attributes);

    final byte[] imageBytes;
    try {//  ww  w.j av a2  s .c  om
        if (width == null) {
            final FileInputStream fis = new FileInputStream(file);
            final ByteArrayOutputStream imageBytesStream = new ByteArrayOutputStream();
            IOUtils.copy(fis, imageBytesStream);
            imageBytes = imageBytesStream.toByteArray();
            IOUtils.closeQuietly(fis);
        } else {
            final BufferedImage bufferedImage = ImageIO.read(file);
            final int oldWidth = bufferedImage.getWidth();
            final int oldHeight = bufferedImage.getHeight();
            final int newWidth = width;
            final int newHeight = newWidth * oldHeight / oldWidth;
            final BufferedImage scaledImage = new BufferedImage(newWidth, newHeight,
                    BufferedImage.TYPE_INT_RGB);
            final Graphics2D g2d = (Graphics2D) scaledImage.getGraphics();
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.drawImage(bufferedImage, 0, 0, newWidth, newHeight, 0, 0, oldWidth, oldHeight, null);
            g2d.dispose();
            final ByteArrayOutputStream imageBytesStream = new ByteArrayOutputStream();
            ImageIO.write(scaledImage, "jpg", imageBytesStream);
            imageBytes = imageBytesStream.toByteArray();
        }
    } catch (final IOException e) {
        throw new WicketRuntimeException(e);
    }

    final ResourceResponse resourceResponse = new ResourceResponse() {
        @Override
        public WriteCallback getWriteCallback() {
            return new WriteCallback() {
                @Override
                public void writeData(final Attributes attributes) {
                    try {
                        this.writeStream(attributes, new ByteArrayInputStream(imageBytes));
                    } catch (IOException e) {
                        throw new WicketRuntimeException(e);
                    }
                }
            };
        }
    };
    resourceResponse.setContentType("image/jpeg");
    resourceResponse.setLastModified(Time.millis(file.lastModified()));
    resourceResponse.setCacheDuration(this.getCacheDuration());
    resourceResponse.setContentLength(imageBytes.length);

    return resourceResponse;
}

From source file:net.dontdrinkandroot.extensions.wicket.resource.FileResourceResponse.java

License:Apache License

public FileResourceResponse(final File file) {
    this.file = file;
    final String contentType = Application.get().getMimeType(this.file.getAbsolutePath());
    this.setContentType(contentType);
    this.setLastModified(Time.millis(this.file.lastModified()));
    this.setContentLength(this.file.length());
}

From source file:net.dontdrinkandroot.wicket.resource.AbstractFileThumbnailResource.java

License:Apache License

@Override
protected ResourceResponse newResourceResponse(final Attributes attributes) {

    final File file = this.resolveFile(attributes);

    final Integer width = this.resolveWidth(attributes);

    final byte[] imageBytes;
    try {//  w  w  w .j  av a 2  s. c  o  m

        if (width == null) {

            final FileInputStream fis = new FileInputStream(file);
            final ByteArrayOutputStream imageBytesStream = new ByteArrayOutputStream();
            IOUtils.copy(fis, imageBytesStream);
            imageBytes = imageBytesStream.toByteArray();
            IOUtils.closeQuietly(fis);

        } else {

            final BufferedImage bufferedImage = ImageIO.read(file);
            final int oldWidth = bufferedImage.getWidth();
            final int oldHeight = bufferedImage.getHeight();
            final int newWidth = width.intValue();
            final int newHeight = newWidth * oldHeight / oldWidth;
            final BufferedImage scaledImage = new BufferedImage(newWidth, newHeight,
                    BufferedImage.TYPE_INT_RGB);
            final Graphics2D g2d = (Graphics2D) scaledImage.getGraphics();
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.drawImage(bufferedImage, 0, 0, newWidth, newHeight, 0, 0, oldWidth, oldHeight, null);
            g2d.dispose();
            final ByteArrayOutputStream imageBytesStream = new ByteArrayOutputStream();
            ImageIO.write(scaledImage, "jpg", imageBytesStream);
            imageBytes = imageBytesStream.toByteArray();

        }

    } catch (final IOException e) {
        throw new WicketRuntimeException(e);
    }

    final ResourceResponse resourceResponse = new ResourceResponse() {

        @Override
        public WriteCallback getWriteCallback() {

            return new WriteCallback() {

                @Override
                public void writeData(final Attributes attributes) {

                    try {
                        this.writeStream(attributes, new ByteArrayInputStream(imageBytes));
                    } catch (IOException e) {
                        throw new WicketRuntimeException(e);
                    }
                }
            };
        }
    };
    resourceResponse.setContentType("image/jpeg");
    resourceResponse.setLastModified(Time.millis(file.lastModified()));
    resourceResponse.setCacheDuration(this.getCacheDuration());
    resourceResponse.setContentLength(imageBytes.length);

    return resourceResponse;
}

From source file:net.dontdrinkandroot.wicket.resource.FileResourceResponse.java

License:Apache License

public FileResourceResponse(final File file) {

    this.file = file;
    final String contentType = Application.get().getMimeType(this.file.getAbsolutePath());
    this.setContentType(contentType);
    this.setLastModified(Time.millis(this.file.lastModified()));
    this.setContentLength(this.file.length());
}

From source file:org.apache.openmeetings.web.util.RecordingResourceReference.java

License:Apache License

@Override
public IResource getResource() {
    return new AbstractResource() {
        private static final long serialVersionUID = 1L;
        private final static String ACCEPT_RANGES_HEADER = "Accept-Ranges";
        private final static String RANGE_HEADER = "Range";
        private final static String CONTENT_RANGE_HEADER = "Content-Range";
        private final static String RANGES_BYTES = "bytes";
        private File file;
        private boolean isRange = false;
        private long start = 0;
        private long end = 0;

        private long getChunkLength() {
            return isRange ? end - start + 1 : (file == null ? -1 : file.length());
        }/*from ww w.jav  a  2 s  .co  m*/

        private IResourceStream getResourceStream() {
            return file == null ? null : new FileResourceStream(file) {
                private static final long serialVersionUID = 2546785247219805747L;
                private transient BoundedInputStream bi;

                @Override
                public InputStream getInputStream() throws ResourceStreamNotFoundException {
                    if (bi == null) {
                        //bi = new BoundedInputStream(super.getInputStream(), end + 1);
                        bi = new BoundedInputStream(super.getInputStream(),
                                isRange ? end + 1 : (file == null ? -1 : file.length()));
                        try {
                            bi.skip(start);
                        } catch (IOException e) {
                            throw new ResourceStreamNotFoundException(e);
                        }
                    }
                    return bi;
                }

                @Override
                public Bytes length() {
                    return Bytes.bytes(getChunkLength());
                }

                @Override
                public void close() throws IOException {
                    if (bi != null) {
                        bi.close(); //also will close original stream
                        bi = null;
                    }
                }

                @Override
                public String getContentType() {
                    return RecordingResourceReference.this.getContentType();
                }
            };
        }

        @Override
        protected void setResponseHeaders(ResourceResponse data, Attributes attributes) {
            Response response = attributes.getResponse();
            if (response instanceof WebResponse) {
                WebResponse webResponse = (WebResponse) response;
                webResponse.setStatus(
                        isRange ? HttpServletResponse.SC_PARTIAL_CONTENT : HttpServletResponse.SC_OK);
            }
            super.setResponseHeaders(data, attributes);
        }

        @Override
        protected ResourceResponse newResourceResponse(Attributes attributes) {
            ResourceResponse rr = new ResourceResponse();
            FlvRecording r = getRecording(attributes);
            if (r != null) {
                isRange = false;
                file = getFile(r);
                rr.setFileName(getFileName(r));
                rr.setContentType(RecordingResourceReference.this.getContentType());
                rr.setContentDisposition(ContentDisposition.INLINE);
                rr.setLastModified(Time.millis(file.lastModified()));
                rr.getHeaders().addHeader(ACCEPT_RANGES_HEADER, RANGES_BYTES);
                String range = ((HttpServletRequest) attributes.getRequest().getContainerRequest())
                        .getHeader(RANGE_HEADER);
                if (range != null && range.startsWith(RANGES_BYTES)) {
                    String[] bounds = range.substring(RANGES_BYTES.length() + 1).split("-");
                    if (bounds != null && bounds.length > 0) {
                        long length = file.length();
                        isRange = true;
                        start = Long.parseLong(bounds[0]);
                        end = bounds.length > 1 ? Long.parseLong(bounds[1]) : length - 1;
                        //Content-Range: bytes 229376-232468/232469
                        rr.getHeaders().addHeader(CONTENT_RANGE_HEADER,
                                String.format("%s %d-%d/%d", RANGES_BYTES, start, end, length));
                    }
                }
                rr.setContentLength(getChunkLength());
                rr.setWriteCallback(new WriteCallback() {
                    @Override
                    public void writeData(Attributes attributes) throws IOException {
                        IResourceStream rStream = getResourceStream();
                        try {
                            writeStream(attributes, rStream.getInputStream());
                        } catch (ResourceStreamNotFoundException e1) {
                        } catch (ResponseIOException e) {
                            // in case of range operations we expecting such exceptions
                            if (!isRange) {
                                log.error("Error while playing the stream", e);
                            }
                        } finally {
                            rStream.close();
                        }
                    }
                });
            } else {
                rr.setError(HttpServletResponse.SC_NOT_FOUND);
            }
            return rr;
        }
    };
}

From source file:org.artifactory.webapp.wicket.application.ArtifactoryWebRequest.java

License:Apache License

@Override
public Time getDateHeader(String name) {
    try {//from   w  w w  .j av  a2s. c o m
        long value = httpServletRequest.getDateHeader(name);

        if (value == -1) {
            return null;
        }

        return Time.millis(value);
    } catch (IllegalArgumentException e) {
        // per spec thrown if the header contains a value that cannot be converted to a date
        return null;
    }
}

From source file:org.cast.cwm.xml.DomCache.java

License:Open Source License

public Time lastUpdated(ICacheableModel<? extends IXmlPointer> mXml, String transform,
        TransformParameters params) {/*from  ww  w . j  a  v  a 2  s  .  c  o m*/
    Element cacheElt = getCache().get(new DOMKey(mXml.getKey(), transform, params));
    long updated = 0;
    if (cacheElt != null) {
        updated = cacheElt.getLastUpdateTime();
        if (updated == 0)
            updated = cacheElt.getCreationTime();
    }
    return (updated == 0 ? null : Time.millis(updated));
}