use of org.apache.wicket.util.resource.ResourceStreamNotFoundException in project wicket by apache.
the class ConcatBundleResource method newResourceResponse.
@Override
protected ResourceResponse newResourceResponse(Attributes attributes) {
final ResourceResponse resourceResponse = new ResourceResponse();
if (resourceResponse.dataNeedsToBeWritten(attributes)) {
try {
List<IResourceStream> resources = collectResourceStreams();
if (resources == null)
return sendResourceError(resourceResponse, HttpServletResponse.SC_NOT_FOUND, "Unable to find resource");
resourceResponse.setContentType(findContentType(resources));
// add Last-Modified header (to support HEAD requests and If-Modified-Since)
final Time lastModified = findLastModified(resources);
if (lastModified != null)
resourceResponse.setLastModified(lastModified);
// read resource data
final byte[] bytes = readAllResources(resources);
// send Content-Length header
resourceResponse.setContentLength(bytes.length);
// send response body with resource data
resourceResponse.setWriteCallback(new WriteCallback() {
@Override
public void writeData(Attributes attributes) {
attributes.getResponse().write(bytes);
}
});
} catch (IOException e) {
log.debug(e.getMessage(), e);
return sendResourceError(resourceResponse, 500, "Unable to read resource stream");
} catch (ResourceStreamNotFoundException e) {
log.debug(e.getMessage(), e);
return sendResourceError(resourceResponse, 500, "Unable to open resource stream");
}
}
return resourceResponse;
}
use of org.apache.wicket.util.resource.ResourceStreamNotFoundException in project wicket by apache.
the class Initializer method getVelocityProperties.
private Properties getVelocityProperties(final Application application) {
String velocityPropertiesFile = "velocity.properties";
if (application instanceof WebApplication) {
WebApplication webapp = (WebApplication) application;
ServletContext servletContext = webapp.getServletContext();
String propertiesFolder = servletContext.getInitParameter("velocityPropertiesFolder");
String propsFile = servletContext.getInitParameter("velocity.properties");
if (null != propsFile) {
velocityPropertiesFile = propsFile;
}
if (null != propertiesFolder) {
WebApplicationPath webPath = new WebApplicationPath(servletContext, propertiesFolder);
IResourceStream stream = webPath.find(Initializer.class, velocityPropertiesFile);
InputStream is;
try {
is = stream.getInputStream();
Properties props = new Properties();
props.load(is);
return props;
} catch (IOException | ResourceStreamNotFoundException e) {
throw new WicketRuntimeException(e);
} finally {
try {
IOUtils.close(stream);
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
}
}
// if it's not a web app, load from the package
InputStream is = Initializer.class.getResourceAsStream("velocity.properties");
try {
Properties props = new Properties();
props.load(is);
return props;
} catch (Exception e) {
throw new WicketRuntimeException(e);
} finally {
try {
IOUtils.close(is);
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
}
use of org.apache.wicket.util.resource.ResourceStreamNotFoundException in project wicket by apache.
the class WebExternalResourceStream method getInputStream.
@Override
public InputStream getInputStream() throws ResourceStreamNotFoundException {
final ServletContext context = ((WebApplication) Application.get()).getServletContext();
in = context.getResourceAsStream(url);
if (in == null) {
throw new ResourceStreamNotFoundException("The requested resource was not found: " + url);
}
return in;
}
use of org.apache.wicket.util.resource.ResourceStreamNotFoundException in project wicket by apache.
the class PackageResource method newResourceResponse.
/**
* creates a new resource response based on the request attributes
*
* @param attributes
* current request attributes from client
* @return resource response for answering request
*/
@Override
protected ResourceResponse newResourceResponse(Attributes attributes) {
final ResourceResponse resourceResponse = new ResourceResponse();
final IResourceStream resourceStream = getResourceStream();
// bail out if resource stream could not be found
if (resourceStream == null) {
return sendResourceError(resourceResponse, HttpServletResponse.SC_NOT_FOUND, "Unable to find resource");
}
// add Last-Modified header (to support HEAD requests and If-Modified-Since)
final Time lastModified = resourceStream.lastModifiedTime();
resourceResponse.setLastModified(lastModified);
if (resourceResponse.dataNeedsToBeWritten(attributes)) {
String contentType = resourceStream.getContentType();
if (contentType == null && Application.exists()) {
contentType = Application.get().getMimeType(path);
}
// set Content-Type (may be null)
resourceResponse.setContentType(contentType);
// set content encoding (may be null)
resourceResponse.setTextEncoding(getTextEncoding());
// supports accept range
resourceResponse.setAcceptRange(ContentRangeType.BYTES);
try {
// read resource data to get the content length
InputStream inputStream = resourceStream.getInputStream();
byte[] bytes = null;
// send Content-Length header
if (readBuffered) {
bytes = IOUtils.toByteArray(inputStream);
resourceResponse.setContentLength(bytes.length);
} else {
resourceResponse.setContentLength(resourceStream.length().bytes());
}
// get content range information
RequestCycle cycle = RequestCycle.get();
Long startbyte = cycle.getMetaData(CONTENT_RANGE_STARTBYTE);
Long endbyte = cycle.getMetaData(CONTENT_RANGE_ENDBYTE);
// send response body with resource data
PartWriterCallback partWriterCallback = new PartWriterCallback(bytes != null ? new ByteArrayInputStream(bytes) : inputStream, resourceResponse.getContentLength(), startbyte, endbyte);
// If read buffered is set to false ensure the part writer callback is going to
// close the input stream
resourceResponse.setWriteCallback(partWriterCallback.setClose(!readBuffered));
} catch (IOException e) {
log.debug(e.getMessage(), e);
return sendResourceError(resourceResponse, 500, "Unable to read resource stream");
} catch (ResourceStreamNotFoundException e) {
log.debug(e.getMessage(), e);
return sendResourceError(resourceResponse, 500, "Unable to open resource stream");
} finally {
try {
if (readBuffered) {
IOUtils.close(resourceStream);
}
} catch (IOException e) {
log.warn("Unable to close the resource stream", e);
}
}
}
return resourceResponse;
}
use of org.apache.wicket.util.resource.ResourceStreamNotFoundException in project wicket by apache.
the class ContextRelativeResource method newResourceResponse.
@Override
protected ResourceResponse newResourceResponse(final Attributes attributes) {
final ResourceResponse resourceResponse = new ResourceResponse();
final WebExternalResourceStream webExternalResourceStream = new WebExternalResourceStream(path);
resourceResponse.setContentType(webExternalResourceStream.getContentType());
resourceResponse.setLastModified(webExternalResourceStream.lastModifiedTime());
resourceResponse.setFileName(path);
resourceResponse.setWriteCallback(new WriteCallback() {
@Override
public void writeData(final Attributes attributes) throws IOException {
try {
InputStream inputStream = webExternalResourceStream.getInputStream();
try {
Streams.copy(inputStream, attributes.getResponse().getOutputStream());
} finally {
IOUtils.closeQuietly(inputStream);
}
} catch (ResourceStreamNotFoundException rsnfx) {
throw new WicketRuntimeException(rsnfx);
}
}
});
return resourceResponse;
}
Aggregations