use of org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey in project scout.rt by eclipse.
the class HttpCacheKeyTest method testEmptyPath.
@Test
public void testEmptyPath() throws Exception {
HttpCacheKey key = new HttpCacheKey("");
Assert.assertEquals("", key.getResourcePath());
Assert.assertEquals(0, key.hashCode());
}
use of org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey in project scout.rt by eclipse.
the class HttpResourceCacheTest method testGet.
@Test
public void testGet() throws Exception {
HttpCacheKey key = new HttpCacheKey("/");
HttpCacheObject obj = rc.get(key);
Assert.assertNull(obj);
}
use of org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey 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.HttpCacheKey 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.HttpCacheKey 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;
}
Aggregations