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();
}
};
}
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));
}
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));
}
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;
}
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;
}
Aggregations