Search in sources :

Example 6 with Bytes

use of org.apache.wicket.util.lang.Bytes in project wicket by apache.

the class SessionSizeDebugPanel method getDataModel.

@Override
protected IModel<String> getDataModel() {
    return new IModel<String>() {

        private static final long serialVersionUID = 1L;

        private final IModel<Bytes> size = new SessionSizeModel();

        @Override
        public String getObject() {
            Bytes sessionSizeInBytes = size.getObject();
            String sessionSizeAsString = sessionSizeInBytes != null ? sessionSizeInBytes.toString() : "unknown";
            return "Session: " + sessionSizeAsString;
        }

        @Override
        public void detach() {
            size.detach();
        }
    };
}
Also used : Bytes(org.apache.wicket.util.lang.Bytes) IModel(org.apache.wicket.model.IModel) SessionSizeModel(org.apache.wicket.devutils.inspector.SessionSizeModel)

Example 7 with Bytes

use of org.apache.wicket.util.lang.Bytes in project wicket by apache.

the class BytesTest method stringOperationsDotLocale.

/**
 * @throws StringValueConversionException
 */
@Test
public void stringOperationsDotLocale() throws StringValueConversionException {
    Locale.setDefault(Locale.UK);
    assertTrue("1G".equals(Bytes.gigabytes(1).toString()));
    assertTrue(Bytes.valueOf("15.5K").bytes() == ((15 * 1024) + 512));
    final Bytes b = Bytes.kilobytes(7.3);
    assertTrue(Bytes.valueOf(b.toString()).equals(b));
}
Also used : Bytes(org.apache.wicket.util.lang.Bytes) Test(org.junit.Test)

Example 8 with Bytes

use of org.apache.wicket.util.lang.Bytes in project wicket by apache.

the class BytesTest method allOperationsExplicitLocale.

/**
 * @throws StringValueConversionException
 */
@Test
public void allOperationsExplicitLocale() throws StringValueConversionException {
    assertTrue("1G".equals(Bytes.gigabytes(1).toString()));
    assertTrue("1,5G".equals(Bytes.gigabytes(1.5).toString(Locale.GERMAN)));
    assertTrue("1.5G".equals(Bytes.gigabytes(1.5).toString(Locale.US)));
    final Bytes b = Bytes.kilobytes(7.3);
    assertEquals(b, Bytes.valueOf(b.toString(Locale.GERMAN), Locale.GERMAN));
    assertTrue(Bytes.valueOf("15,5K", Locale.GERMAN).bytes() == ((15 * 1024) + 512));
    assertTrue(Bytes.valueOf("15.5K", Locale.US).bytes() == ((15 * 1024) + 512));
}
Also used : Bytes(org.apache.wicket.util.lang.Bytes) Test(org.junit.Test)

Example 9 with Bytes

use of org.apache.wicket.util.lang.Bytes in project wicket by apache.

the class MultipartServletWebRequestImpl method newMultipartWebRequest.

@Override
public MultipartServletWebRequest newMultipartWebRequest(Bytes maxSize, String upload) throws FileUploadException {
    // FIXME mgrigorov: Why these checks are made here ?!
    // Why they are not done also at org.apache.wicket.protocol.http.servlet.MultipartServletWebRequestImpl.newMultipartWebRequest(org.apache.wicket.util.lang.Bytes, java.lang.String, org.apache.wicket.util.upload.FileItemFactory)() ?
    // Why there is no check that the summary of all files' sizes is less than the set maxSize ?
    // Setting a breakpoint here never breaks with the standard upload examples.
    Bytes fileMaxSize = getFileMaxSize();
    for (Map.Entry<String, List<FileItem>> entry : files.entrySet()) {
        List<FileItem> fileItems = entry.getValue();
        for (FileItem fileItem : fileItems) {
            if (fileMaxSize != null && fileItem.getSize() > fileMaxSize.bytes()) {
                String fieldName = entry.getKey();
                FileUploadException fslex = new FileUploadBase.FileSizeLimitExceededException("The field '" + fieldName + "' exceeds its maximum permitted size of '" + maxSize + "' characters.", fileItem.getSize(), fileMaxSize.bytes());
                throw fslex;
            }
        }
    }
    return this;
}
Also used : Bytes(org.apache.wicket.util.lang.Bytes) FileItem(org.apache.commons.fileupload.FileItem) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) ValueMap(org.apache.wicket.util.value.ValueMap) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 10 with Bytes

use of org.apache.wicket.util.lang.Bytes in project wicket by apache.

the class ResourceStreamResource method newResourceResponse.

@Override
protected ResourceResponse newResourceResponse(Attributes attributes) {
    final IResourceStream resourceStream = internalGetResourceStream(attributes);
    ResourceResponse data = new ResourceResponse();
    Time lastModifiedTime = resourceStream.lastModifiedTime();
    if (lastModifiedTime != null) {
        data.setLastModified(lastModifiedTime);
    }
    if (cacheDuration != null) {
        data.setCacheDuration(cacheDuration);
    }
    // performance check; don't bother to do anything if the resource is still cached by client
    if (data.dataNeedsToBeWritten(attributes)) {
        InputStream inputStream = null;
        if (resourceStream instanceof IResourceStreamWriter == false) {
            try {
                inputStream = resourceStream.getInputStream();
            } catch (ResourceStreamNotFoundException e) {
                data.setError(HttpServletResponse.SC_NOT_FOUND);
                close(resourceStream);
            }
        }
        data.setContentDisposition(contentDisposition);
        Bytes length = resourceStream.length();
        if (length != null) {
            data.setContentLength(length.bytes());
        }
        data.setFileName(fileName);
        String contentType = resourceStream.getContentType();
        if (contentType == null && fileName != null && Application.exists()) {
            contentType = Application.get().getMimeType(fileName);
        }
        data.setContentType(contentType);
        data.setTextEncoding(textEncoding);
        if (resourceStream instanceof IResourceStreamWriter) {
            data.setWriteCallback(new WriteCallback() {

                @Override
                public void writeData(Attributes attributes) throws IOException {
                    ((IResourceStreamWriter) resourceStream).write(attributes.getResponse().getOutputStream());
                    close(resourceStream);
                }
            });
        } else {
            final InputStream s = inputStream;
            data.setWriteCallback(new WriteCallback() {

                @Override
                public void writeData(Attributes attributes) throws IOException {
                    try {
                        writeStream(attributes, s);
                    } finally {
                        close(resourceStream);
                    }
                }
            });
        }
    }
    return data;
}
Also used : Bytes(org.apache.wicket.util.lang.Bytes) IResourceStream(org.apache.wicket.util.resource.IResourceStream) InputStream(java.io.InputStream) Time(org.apache.wicket.util.time.Time) IOException(java.io.IOException) ResourceStreamNotFoundException(org.apache.wicket.util.resource.ResourceStreamNotFoundException) IResourceStreamWriter(org.apache.wicket.util.resource.IResourceStreamWriter)

Aggregations

Bytes (org.apache.wicket.util.lang.Bytes)12 Test (org.junit.Test)5 File (java.io.File)2 IModel (org.apache.wicket.model.IModel)2 StoreSettings (org.apache.wicket.settings.StoreSettings)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 FileItem (org.apache.commons.fileupload.FileItem)1 FileUploadException (org.apache.commons.fileupload.FileUploadException)1 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)1 Page (org.apache.wicket.Page)1 UrlResourceStream (org.apache.wicket.core.util.resource.UrlResourceStream)1 SessionSizeModel (org.apache.wicket.devutils.inspector.SessionSizeModel)1 WebPage (org.apache.wicket.markup.html.WebPage)1 DiskDataStore (org.apache.wicket.pageStore.DiskDataStore)1