use of org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject in project scout.rt by eclipse.
the class HttpResourceCacheTest method testPutCachable.
@Test
public void testPutCachable() throws Exception {
BinaryResource res = BinaryResources.create().withFilename("a.html").withContent("<html></html>".getBytes("UTF-8")).withCachingAllowed(true).build();
HttpCacheObject obj = new HttpCacheObject(new HttpCacheKey("/"), res);
boolean b = rc.put(obj);
Assert.assertTrue(b);
}
use of org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject in project scout.rt by eclipse.
the class HttpResourceCacheTest method testPutGet.
@Test
public void testPutGet() throws Exception {
BinaryResource res = BinaryResources.create().withFilename("a.html").withContent("<html></html>".getBytes("UTF-8")).withCachingAllowed(true).build();
HttpCacheObject obj = new HttpCacheObject(new HttpCacheKey("/"), res);
boolean b = rc.put(obj);
Assert.assertTrue(b);
HttpCacheObject obj2 = rc.get(new HttpCacheKey("/"));
Assert.assertEquals(obj.getCacheKey(), obj2.getCacheKey());
}
use of org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject in project scout.rt by eclipse.
the class ResourceServlet method writeStaticResource.
private boolean writeStaticResource(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
String pathInfo = req.getPathInfo();
if (StringUtility.isNullOrEmpty(pathInfo) || pathInfo.endsWith("/")) {
pathInfo = "/index.html";
}
URL url = null;
String contentType = null;
if (m_warPath != null) {
ServletContext servletContext = getServletContext();
String resourcePath = m_warPath + pathInfo;
url = servletContext.getResource(resourcePath);
contentType = FileUtility.getMimeType(resourcePath);
}
//
if (url == null) {
return false;
}
long lastModified;
int contentLength;
URLConnection connection = url.openConnection();
lastModified = connection.getLastModified();
contentLength = connection.getContentLength();
BinaryResource res = BinaryResources.create().withFilename(pathInfo).withContentType(contentType).withLastModified(lastModified).build();
HttpCacheObject obj = new HttpCacheObject(new HttpCacheKey(pathInfo), res);
if (BEANS.get(HttpCacheControl.class).checkAndSetCacheHeaders(req, resp, obj)) {
return true;
}
try (InputStream is = connection.getInputStream()) {
@SuppressWarnings("resource") OutputStream os = resp.getOutputStream();
byte[] buffer = new byte[8192];
int bytesRead = is.read(buffer);
int writtenContentLength = 0;
while (bytesRead != -1) {
os.write(buffer, 0, bytesRead);
writtenContentLength += bytesRead;
bytesRead = is.read(buffer);
}
if (contentLength == -1 || contentLength != writtenContentLength) {
resp.setContentLength(writtenContentLength);
}
}
return true;
}
use of org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject in project scout.rt by eclipse.
the class RemoteFileServlet method writeResource.
private boolean writeResource(final HttpServletRequest req, final HttpServletResponse resp, final String resourcePath) throws IOException {
IRemoteFileService rfs = BEANS.get(getConfiguredRemoteFileServiceClass());
RemoteFile spec = new RemoteFile((resourcePath == null) ? null : StringUtility.join("", m_folder, resourcePath), -1);
RemoteFile remoteFile = rfs.getRemoteFile(spec);
if (!remoteFile.exists()) {
return false;
}
HttpCacheObject obj = new HttpCacheObject(new HttpCacheKey(resourcePath), remoteFile.toBinaryResource());
if (BEANS.get(HttpCacheControl.class).checkAndSetCacheHeaders(req, resp, obj)) {
return true;
}
rfs.streamRemoteFile(remoteFile, resp.getOutputStream());
return true;
}
use of org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject in project scout.rt by eclipse.
the class DynamicResourceLoader method loadResource.
@Override
public HttpCacheObject loadResource(HttpCacheKey cacheKey) {
DynamicResourceInfo info = createDynamicResourceInfo(cacheKey);
if (info == null) {
LOG.warn("invalid dynamic-resource request received.", new Exception("origin"));
return null;
}
IBinaryResourceProvider provider = getBinaryResourceProvider(info.getUiSession(), info.getJsonAdapterId());
if (provider == null) {
return null;
}
BinaryResourceHolder localResourceHolder = provider.provideBinaryResource(info.getFileName());
if (localResourceHolder == null || localResourceHolder.get() == null) {
return null;
}
BinaryResource localResource = localResourceHolder.get();
BinaryResource httpResource = localResource.createAlias(cacheKey.getResourcePath());
HttpCacheObject httpCacheObject = new HttpCacheObject(cacheKey, httpResource);
for (IHttpResponseInterceptor interceptor : localResourceHolder.getHttpResponseInterceptors()) {
httpCacheObject.addHttpResponseInterceptor(interceptor);
}
return httpCacheObject;
}
Aggregations