|
From: <tre...@us...> - 2007-07-03 18:45:16
|
Revision: 211
http://ogoglio.svn.sourceforge.net/ogoglio/?rev=211&view=rev
Author: trevorolio
Date: 2007-07-03 11:45:13 -0700 (Tue, 03 Jul 2007)
Log Message:
-----------
Added DecoratedInputStream, which is a wrapper for InputStream with additional metadata like size, age, and mimetype.
Made the media service produce DecoratedInputStreams.
Made the media servlet add content length, type, last-modified headers based on decorations.
Made the TemplateResource respond to head with length and last-modified.
Modified Paths:
--------------
spaces/trunk/src/com/ogoglio/client/WebAPIClient.java
spaces/trunk/src/com/ogoglio/media/FileStore.java
spaces/trunk/src/com/ogoglio/media/MediaService.java
spaces/trunk/src/com/ogoglio/media/MediaStore.java
spaces/trunk/src/com/ogoglio/media/WebStore.java
spaces/trunk/src/com/ogoglio/media/site/MediaServlet.java
spaces/trunk/src/com/ogoglio/site/AbstractResourceServlet.java
spaces/trunk/src/com/ogoglio/site/AccountServlet.java
spaces/trunk/src/com/ogoglio/site/DescendingSiteResource.java
Added Paths:
-----------
spaces/trunk/src/com/ogoglio/client/DecoratedInputStream.java
Added: spaces/trunk/src/com/ogoglio/client/DecoratedInputStream.java
===================================================================
--- spaces/trunk/src/com/ogoglio/client/DecoratedInputStream.java (rev 0)
+++ spaces/trunk/src/com/ogoglio/client/DecoratedInputStream.java 2007-07-03 18:45:13 UTC (rev 211)
@@ -0,0 +1,81 @@
+package com.ogoglio.client;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import com.ogoglio.util.ArgumentUtils;
+
+public class DecoratedInputStream extends InputStream {
+
+ private InputStream stream = null;
+
+ private long length = -1;
+
+ private String mimeType = null;
+
+ private long lastModified = -1;
+
+ public DecoratedInputStream(InputStream stream, long length, String mimeType, long lastModified) {
+ ArgumentUtils.assertNotNull(stream);
+ this.stream = stream;
+ this.length = length;
+ this.mimeType = mimeType;
+ this.lastModified = lastModified;
+ }
+
+ public long getLength() {
+ return length;
+ }
+
+ public String getMimeType() {
+ return mimeType;
+ }
+
+ public long getLastModified() {
+ return lastModified;
+ }
+
+ public int read() throws IOException{
+ return stream.read();
+ }
+
+ public int read(byte[] b) throws IOException {
+ return stream.read(b);
+ }
+
+ public int read(byte[] b,
+ int off,
+ int len)
+ throws IOException{
+ return stream.read(b, off, len);
+ }
+
+ public long skip(long n)
+ throws IOException{
+ return stream.skip(n);
+ }
+
+ public int available()
+ throws IOException{
+ return stream.available();
+ }
+
+ public void close()
+ throws IOException{
+ stream.close();
+ }
+
+ public void mark(int readlimit) {
+ stream.mark(readlimit);
+ }
+
+ public void reset()
+ throws IOException{
+ stream.reset();
+ }
+
+ public boolean markSupported() {
+ return stream.markSupported();
+ }
+
+}
Modified: spaces/trunk/src/com/ogoglio/client/WebAPIClient.java
===================================================================
--- spaces/trunk/src/com/ogoglio/client/WebAPIClient.java 2007-07-03 17:16:54 UTC (rev 210)
+++ spaces/trunk/src/com/ogoglio/client/WebAPIClient.java 2007-07-03 18:45:13 UTC (rev 211)
@@ -20,7 +20,7 @@
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URI;
-import java.net.URISyntaxException;
+import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -296,7 +296,6 @@
return StreamUtils.readInput(input);
}
-
public SpaceDocument getSpaceDocument(boolean includeChildren) throws IOException {
return new SpaceDocument(fetchAuthenticatedXML(getSpaceURI(includeChildren)));
}
@@ -499,7 +498,7 @@
return fetchAuthenticatedStream(WebAPIUtil.appendToURI(renderableRootURI, "geometry/data/" + lodIndex), authCookie);
}
- public static InputStream performGET(URI uri, String authCookie) throws IOException {
+ public static DecoratedInputStream performGET(URI uri, String authCookie) throws IOException {
HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
connection.setRequestMethod("GET");
connection.setAllowUserInteraction(false);
@@ -512,9 +511,9 @@
throw new IOException("Get status " + connection.getResponseCode() + " from " + uri);
}
if ("gzip".equals(connection.getHeaderField("Content-encoding"))) {
- return new GZIPInputStream(connection.getInputStream());
+ return new DecoratedInputStream(new GZIPInputStream(connection.getInputStream()), connection.getContentLength(), connection.getContentType(), connection.getLastModified());
} else {
- return connection.getInputStream();
+ return new DecoratedInputStream(connection.getInputStream(), connection.getContentLength(), connection.getContentType(), connection.getLastModified());
}
}
@@ -522,7 +521,19 @@
return sendAuthenticatedXML(uri, body, "POST", authCookie);
}
+ public static long requestLastModified(URI uri, String authCookie) {
+ return getHeadDate(uri, authCookie, "Last-Modified");
+ }
+
public static long requestContentLength(URI uri, String authCookie) {
+ String lastModValue = getHeadValue(uri, authCookie, "Content-Length");
+ if(lastModValue == null) {
+ return -1;
+ }
+ return Long.parseLong(lastModValue);
+ }
+
+ private static long getHeadDate(URI uri, String authCookie, String headerName) {
try {
HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
connection.setRequestMethod("HEAD");
@@ -530,30 +541,47 @@
if (authCookie != null) {
connection.setRequestProperty("Cookie", AuthServlet.AUTH_COOKIE + "=" + authCookie);
}
-
connection.setDoOutput(false);
if (connection.getResponseCode() != 200) {
return -1;
}
- // XXX get("Content-Length") on linux (1.5.0_07) returns a List so this hideous workaround
+ return connection.getHeaderFieldDate(headerName, -1);
+ } catch (IOException e) {
+ return -1;
+ }
+ }
+
+ private static String getHeadValue(URI uri, String authCookie, String headerName) {
+ try {
+ HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
+ connection.setRequestMethod("HEAD");
+ connection.setAllowUserInteraction(false);
+ if (authCookie != null) {
+ connection.setRequestProperty("Cookie", AuthServlet.AUTH_COOKIE + "=" + authCookie);
+ }
+ connection.setDoOutput(false);
+ if (connection.getResponseCode() != 200) {
+ return null;
+ }
+ // XXX get(...) on linux (1.5.0_07) returns a List so this hideous workaround
// XXX seems to be required
- String lengthHeader=null;
- Object makeLinuxHappy=(connection.getRequestProperties().get("Content-Length"));
+ String headerValue = null;
+ Object makeLinuxHappy = (connection.getRequestProperties().get(headerName));
if (makeLinuxHappy instanceof String) {
- lengthHeader = (String)makeLinuxHappy;
+ headerValue = (String) makeLinuxHappy;
} else if (makeLinuxHappy instanceof List) {
- lengthHeader = (String) ((List)makeLinuxHappy).get(0);
+ headerValue = (String) ((List) makeLinuxHappy).get(0);
} else {
- // we don't understand this type at all
- System.err.println("Unable to understand the type returned by Linux workaround in WebAPIClient!");
- return -1;
+ // we don't understand this type at all
+ System.err.println("Unable to understand the type returned by Linux workaround in WebAPIClient!");
+ return null;
}
- if(lengthHeader == null) {
- return -1;
+ if (headerValue == null) {
+ return null;
}
- return Integer.parseInt(lengthHeader);
+ return headerValue;
} catch (IOException e) {
- return -1;
+ return null;
}
}
Modified: spaces/trunk/src/com/ogoglio/media/FileStore.java
===================================================================
--- spaces/trunk/src/com/ogoglio/media/FileStore.java 2007-07-03 17:16:54 UTC (rev 210)
+++ spaces/trunk/src/com/ogoglio/media/FileStore.java 2007-07-03 18:45:13 UTC (rev 211)
@@ -20,6 +20,7 @@
import java.io.InputStream;
import java.net.URI;
+import com.ogoglio.client.DecoratedInputStream;
import com.ogoglio.util.ArgumentUtils;
import com.ogoglio.util.StreamUtils;
@@ -66,11 +67,16 @@
return true;
}
- public InputStream getData(String name) {
+ public DecoratedInputStream getData(String name) {
ArgumentUtils.assertNotEmpty(name);
ArgumentUtils.assertNoPaths(name);
try {
- return new FileInputStream(new File(mediaDir, name));
+ File file = new File(mediaDir, name);
+ if(!file.exists() || !file.isFile()) {
+ return null;
+ }
+ //TODO use one of the filename to mimetype utils to decorate this stream
+ return new DecoratedInputStream(new FileInputStream(file), file.length(), null, file.lastModified());
} catch (IOException e) {
return null;
}
@@ -91,6 +97,16 @@
return -1;
}
+ public long getLastModified(String name) throws IOException {
+ ArgumentUtils.assertNotEmpty(name);
+ ArgumentUtils.assertNoPaths(name);
+ File file = new File(mediaDir, name);
+ if (file.exists() && file.isFile()) {
+ return file.lastModified();
+ }
+ return -1;
+ }
+
public boolean delete(String name) {
ArgumentUtils.assertNotEmpty(name);
ArgumentUtils.assertNoPaths(name);
@@ -100,5 +116,4 @@
}
return file.delete();
}
-
}
Modified: spaces/trunk/src/com/ogoglio/media/MediaService.java
===================================================================
--- spaces/trunk/src/com/ogoglio/media/MediaService.java 2007-07-03 17:16:54 UTC (rev 210)
+++ spaces/trunk/src/com/ogoglio/media/MediaService.java 2007-07-03 18:45:13 UTC (rev 211)
@@ -17,6 +17,7 @@
import java.io.InputStream;
import java.net.URI;
+import com.ogoglio.client.DecoratedInputStream;
import com.ogoglio.util.ArgumentUtils;
public class MediaService {
@@ -85,7 +86,7 @@
return store.write(name, input, maxSize);
}
- public InputStream getData(String name) {
+ public DecoratedInputStream getData(String name) {
return store.getData(name);
}
@@ -93,6 +94,10 @@
return store.getSize(name);
}
+ public long getLastModified(String name) throws IOException {
+ return store.getLastModified(name);
+ }
+
public boolean delete(String name) {
return store.delete(name);
}
Modified: spaces/trunk/src/com/ogoglio/media/MediaStore.java
===================================================================
--- spaces/trunk/src/com/ogoglio/media/MediaStore.java 2007-07-03 17:16:54 UTC (rev 210)
+++ spaces/trunk/src/com/ogoglio/media/MediaStore.java 2007-07-03 18:45:13 UTC (rev 211)
@@ -16,6 +16,8 @@
import java.io.IOException;
import java.io.InputStream;
+import com.ogoglio.client.DecoratedInputStream;
+
public interface MediaStore {
public boolean write(String name, String value) throws IOException;
@@ -24,7 +26,9 @@
public long getSize(String name) throws IOException;
- public InputStream getData(String name);
+ public long getLastModified(String name) throws IOException;
+
+ public DecoratedInputStream getData(String name);
public boolean delete(String name);
Modified: spaces/trunk/src/com/ogoglio/media/WebStore.java
===================================================================
--- spaces/trunk/src/com/ogoglio/media/WebStore.java 2007-07-03 17:16:54 UTC (rev 210)
+++ spaces/trunk/src/com/ogoglio/media/WebStore.java 2007-07-03 18:45:13 UTC (rev 211)
@@ -1,16 +1,16 @@
/* Copyright 2007 Transmutable (http://transmutable.com/)
-Licensed under the Apache License, Version 2.0 (the "License");
-you may not use this file except in compliance with the License.
-You may obtain a copy of the License at
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
-http://www.apache.org/licenses/LICENSE-2.0
+ http://www.apache.org/licenses/LICENSE-2.0
-Unless required by applicable law or agreed to in writing, software
-distributed under the License is distributed on an "AS IS" BASIS,
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-See the License for the specific language governing permissions and
-limitations under the License. */
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License. */
package com.ogoglio.media;
import java.io.ByteArrayInputStream;
@@ -18,6 +18,7 @@
import java.io.InputStream;
import java.net.URI;
+import com.ogoglio.client.DecoratedInputStream;
import com.ogoglio.client.WebAPIClient;
import com.ogoglio.client.WebAPIUtil;
import com.ogoglio.util.ArgumentUtils;
@@ -25,44 +26,48 @@
public class WebStore implements MediaStore {
URI mediaURI = null;
-
- public WebStore(URI mediaURI) throws IOException {
- ArgumentUtils.assertNotNull(mediaURI);
- this.mediaURI = mediaURI;
- }
- public boolean write(String name, String value) throws IOException {
- return write(name, new ByteArrayInputStream(value.getBytes()), -1);
- }
+ public WebStore(URI mediaURI) throws IOException {
+ ArgumentUtils.assertNotNull(mediaURI);
+ this.mediaURI = mediaURI;
+ }
- public boolean write(String name, InputStream input, long maxSize) throws IOException {
- InputStream responseInput = WebAPIClient.performPUT(WebAPIUtil.appendToURI(mediaURI, name), input, "application/data", -1, null);
- if(responseInput == null) {
- return false;
- }
- responseInput.close();
+ public boolean write(String name, String value) throws IOException {
+ return write(name, new ByteArrayInputStream(value.getBytes()), -1);
+ }
- return true;
- }
+ public boolean write(String name, InputStream input, long maxSize) throws IOException {
+ InputStream responseInput = WebAPIClient.performPUT(WebAPIUtil.appendToURI(mediaURI, name), input, "application/data", -1, null);
+ if (responseInput == null) {
+ return false;
+ }
+ responseInput.close();
- public InputStream getData(String name) {
- try {
- return WebAPIClient.performGET(WebAPIUtil.appendToURI(mediaURI, name), null);
- } catch (IOException e) {
- return null;
- }
- }
+ return true;
+ }
- public long getSize(String name) {
- return WebAPIClient.requestContentLength(WebAPIUtil.appendToURI(mediaURI, name), null);
- }
+ public DecoratedInputStream getData(String name) {
+ try {
+ return WebAPIClient.performGET(WebAPIUtil.appendToURI(mediaURI, name), null);
+ } catch (IOException e) {
+ return null;
+ }
+ }
- public boolean delete(String name) {
- try {
- return WebAPIClient.sendDelete(WebAPIUtil.appendToURI(mediaURI, name), null);
- } catch (IOException e) {
- e.printStackTrace();
- return false;
- }
- }
+ public long getSize(String name) {
+ return WebAPIClient.requestContentLength(WebAPIUtil.appendToURI(mediaURI, name), null);
+ }
+
+ public long getLastModified(String name) throws IOException {
+ return WebAPIClient.requestLastModified(WebAPIUtil.appendToURI(mediaURI, name), null);
+ }
+
+ public boolean delete(String name) {
+ try {
+ return WebAPIClient.sendDelete(WebAPIUtil.appendToURI(mediaURI, name), null);
+ } catch (IOException e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
}
Modified: spaces/trunk/src/com/ogoglio/media/site/MediaServlet.java
===================================================================
--- spaces/trunk/src/com/ogoglio/media/site/MediaServlet.java 2007-07-03 17:16:54 UTC (rev 210)
+++ spaces/trunk/src/com/ogoglio/media/site/MediaServlet.java 2007-07-03 18:45:13 UTC (rev 211)
@@ -2,7 +2,6 @@
import java.io.File;
import java.io.IOException;
-import java.io.InputStream;
import java.util.Date;
import javax.servlet.ServletConfig;
@@ -10,6 +9,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import com.ogoglio.client.DecoratedInputStream;
import com.ogoglio.media.FileStore;
import com.ogoglio.site.AbstractResourceServlet;
import com.ogoglio.site.SiteResource;
@@ -40,7 +40,7 @@
private class MediaResource extends SiteResource {
public MediaResource() {
super("media");
- addSubResource(new DataResource());
+ addSubResource(new DataResource());
}
public void doGet(HttpServletRequest request, HttpServletResponse response, String[] pathElements) throws ServletException, IOException {
@@ -54,28 +54,33 @@
}
public void doGet(HttpServletRequest request, HttpServletResponse response, String[] pathElements) throws ServletException, IOException {
- if(fileStore == null) {
+ if (fileStore == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
String name = pathElements[pathElements.length - 1];
- InputStream input = fileStore.getData(name);
+ DecoratedInputStream input = fileStore.getData(name);
if (input == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
response.setStatus(HttpServletResponse.SC_OK);
- response.setContentLength((int) fileStore.getSize(name));
+ if (input.getLength() > -1) {
+ response.setContentLength((int) input.getLength());
+ }
+ if(input.getMimeType() != null) {
+ response.setContentType(input.getMimeType());
+ }
StreamUtils.write(input, response.getOutputStream());
}
public void doPut(HttpServletRequest request, HttpServletResponse response, String[] pathElements) throws ServletException, IOException {
- if(fileStore == null) {
+ if (fileStore == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
-
+
String name = pathElements[pathElements.length - 1];
fileStore.write(name, request.getInputStream(), -1);
response.setStatus(HttpServletResponse.SC_OK);
@@ -84,7 +89,7 @@
}
public void doDelete(HttpServletRequest request, HttpServletResponse response, String[] pathElements) throws ServletException, IOException {
- if(fileStore == null) {
+ if (fileStore == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
@@ -108,7 +113,7 @@
}
public void doHead(HttpServletRequest request, HttpServletResponse response, String[] pathElements) throws ServletException, IOException {
- if(fileStore == null) {
+ if (fileStore == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
@@ -122,6 +127,7 @@
response.setStatus(HttpServletResponse.SC_OK);
response.setContentLength((int) fileStore.getSize(name));
+ response.setDateHeader("Last-Modified", fileStore.getLastModified(name));
return;
}
}
Modified: spaces/trunk/src/com/ogoglio/site/AbstractResourceServlet.java
===================================================================
--- spaces/trunk/src/com/ogoglio/site/AbstractResourceServlet.java 2007-07-03 17:16:54 UTC (rev 210)
+++ spaces/trunk/src/com/ogoglio/site/AbstractResourceServlet.java 2007-07-03 18:45:13 UTC (rev 211)
@@ -130,13 +130,17 @@
public static void setNoCache(HttpServletResponse response) {
//IE caches XMLHttpRequest responses, so we set explicit no-cache headers
- response.setHeader("Pragma", "no-cache");
response.addHeader("Cache-Control", "must-revalidate");
response.addHeader("Cache-Control", "no-cache");
response.addHeader("Cache-Control", "no-store");
response.setDateHeader("Expires", 0);
}
+ public static void setCachable(HttpServletResponse response) {
+ response.setHeader("Cache-Control", "public");
+ response.setDateHeader("Expires", System.currentTimeMillis() + 10000000);
+ }
+
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (noCache) {
setNoCache(response);
Modified: spaces/trunk/src/com/ogoglio/site/AccountServlet.java
===================================================================
--- spaces/trunk/src/com/ogoglio/site/AccountServlet.java 2007-07-03 17:16:54 UTC (rev 210)
+++ spaces/trunk/src/com/ogoglio/site/AccountServlet.java 2007-07-03 18:45:13 UTC (rev 211)
@@ -28,6 +28,7 @@
import nanoxml.XMLElement;
+import com.ogoglio.client.DecoratedInputStream;
import com.ogoglio.client.WebAPIClient;
import com.ogoglio.client.WebAPIUtil;
import com.ogoglio.media.MediaService;
@@ -81,7 +82,7 @@
super.init(config);
try {
ServiceInitializationPersistTasks.initializeLocalSim(new URI(getSiteInfo().getBaseUrl()), getSessionFactory());
- ServiceInitializationPersistTasks.initializeLibraryAccount(getSessionFactory(),getSiteInfo().getHost());
+ ServiceInitializationPersistTasks.initializeLibraryAccount(getSessionFactory(), getSiteInfo().getHost());
} catch (PersistException e) {
e.printStackTrace();
throw new ServletException("Could not initialize service: " + e);
@@ -101,7 +102,7 @@
private class AccountsResource extends AuthenticatedSiteResource {
public AccountsResource() {
- super("account", false, getSessionFactory());
+ super("account", false, getSessionFactory());
addSubResource(new TemplateQueryResource());
addSubResource(new AccountResource());
}
@@ -110,8 +111,8 @@
sendStringResponse("Nothing to see here, folks.\n", "text/plain", response);
}
- public void doPost(HttpServletRequest request, HttpServletResponse response, String[] pathElements, AccountRecord authedAccount) throws ServletException, IOException, PersistException {
- if(authedAccount == null || !AccountRecord.ACCOUNT_LEVEL_ADMIN.equals(authedAccount.getAccountlevel())) {
+ public void doPost(HttpServletRequest request, HttpServletResponse response, String[] pathElements, AccountRecord authedAccount) throws ServletException, IOException, PersistException {
+ if (authedAccount == null || !AccountRecord.ACCOUNT_LEVEL_ADMIN.equals(authedAccount.getAccountlevel())) {
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
return;
}
@@ -194,26 +195,26 @@
return;
}
- if(!AccountRecord.ACCOUNT_LEVEL_ADMIN.equals(authedAccount.getAccountlevel())) {
- if(updatedDocument.getAccountLevel() != null && !requestedAccount.getAccountlevel().equals(updatedDocument.getAccountLevel())) {
+ if (!AccountRecord.ACCOUNT_LEVEL_ADMIN.equals(authedAccount.getAccountlevel())) {
+ if (updatedDocument.getAccountLevel() != null && !requestedAccount.getAccountlevel().equals(updatedDocument.getAccountLevel())) {
System.err.println("User tried to update own account level: " + requestedAccount.getUsername());
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
return;
}
- if(requestedAccount.getFrozenUntil() != null && !requestedAccount.getFrozenUntil().equals(updatedDocument.getFrozenUntil())) {
+ if (requestedAccount.getFrozenUntil() != null && !requestedAccount.getFrozenUntil().equals(updatedDocument.getFrozenUntil())) {
System.err.println("User tried to update own freeze: " + requestedAccount.getUsername());
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
return;
}
- if(requestedAccount.getFrozenUntil() == null && updatedDocument.getFrozenUntil() != null) {
+ if (requestedAccount.getFrozenUntil() == null && updatedDocument.getFrozenUntil() != null) {
System.err.println("User tried to freeze themselves: " + requestedAccount.getUsername());
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
return;
}
}
-
+
//TODO if this returns false we really should reflect that in the response
-
+
AccountPersistTasks.update(requestedAccount, updatedDocument, getSessionFactory());
AccountDocument result = createAccountDocument(requestedAccount, true);
@@ -402,7 +403,7 @@
return;
}
- InputStream data = null;
+ DecoratedInputStream data = null;
if (pathElements.length == 7 && "data".equals(pathElements[pathElements.length - 2])) {
data = getMediaService().getData(MediaService.getTemplateGeometryName(templateID, Integer.parseInt(pathElements[pathElements.length - 1])));
} else {
@@ -413,6 +414,13 @@
return;
}
response.setStatus(HttpServletResponse.SC_OK);
+ setCachable(response);
+ if (data.getLength() > -1) {
+ response.setContentLength((int) data.getLength());
+ }
+ if (data.getMimeType() != null) {
+ response.setContentType(data.getMimeType());
+ }
if ("gzip".equals(request.getHeader("Accept-encoding"))) {
response.setHeader("Content-encoding", "gzip");
StreamUtils.write(data, new GZIPOutputStream(response.getOutputStream()));
@@ -425,6 +433,59 @@
return;
}
}
+
+ public void doHead(HttpServletRequest request, HttpServletResponse response, String[] pathElements) throws ServletException, IOException {
+ String usernameParam = pathElements[1];
+ try {
+ AccountRecord authedAccount = AuthServlet.getAuthedAccountRecord(request, getSessionFactory());
+ if (authedAccount == null && !AuthServlet.isGuest(request)) {
+ response.setStatus(HttpServletResponse.SC_FORBIDDEN);
+ return;
+ }
+
+ AccountRecord requestedAccount = AccountPersistTasks.findAccountByUsername(usernameParam, getSessionFactory());
+ if (requestedAccount == null) {
+ response.setStatus(HttpServletResponse.SC_NOT_FOUND);
+ return;
+ }
+
+ long templateID = Long.parseLong(pathElements[3]);
+ TemplateRecord record = TemplatePersistTasks.findTemplateByTemplateID(templateID, getSessionFactory());
+ if (record == null || !record.getOwnerUsername().equals(requestedAccount.getUsername())) {
+ response.setStatus(HttpServletResponse.SC_NOT_FOUND);
+ return;
+ }
+
+ if (pathElements.length == 5) {
+ sendStringResponse("This is where a geometry document would be.", "text/plain", response);
+ return;
+ }
+
+ String mediaName = null;
+ if (pathElements.length == 7 && "data".equals(pathElements[pathElements.length - 2])) {
+ mediaName = MediaService.getTemplateGeometryName(templateID, Integer.parseInt(pathElements[pathElements.length - 1]));
+ } else {
+ mediaName = MediaService.getTemplateResourceName(templateID, pathElements[pathElements.length - 1]);
+ }
+
+ long length = getMediaService().getSize(mediaName);
+ if (length == -1) {
+ response.setStatus(HttpServletResponse.SC_NOT_FOUND);
+ return;
+ }
+
+ long lastModified = getMediaService().getLastModified(mediaName);
+
+ setCachable(response);
+ response.setStatus(HttpServletResponse.SC_OK);
+ response.setContentLength((int) length);
+ response.setDateHeader("Last-Modified", lastModified);
+ } catch (PersistException e) {
+ e.printStackTrace();
+ response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+ return;
+ }
+ }
}
private class TemplateScriptResource extends AuthenticatedSiteResource {
Modified: spaces/trunk/src/com/ogoglio/site/DescendingSiteResource.java
===================================================================
--- spaces/trunk/src/com/ogoglio/site/DescendingSiteResource.java 2007-07-03 17:16:54 UTC (rev 210)
+++ spaces/trunk/src/com/ogoglio/site/DescendingSiteResource.java 2007-07-03 18:45:13 UTC (rev 211)
@@ -60,5 +60,9 @@
public void doDelete(HttpServletRequest request, HttpServletResponse response, String[] pathElements) throws ServletException, IOException {
DescendingSiteResource.this.doDelete(request, response, pathElements);
}
- }
+
+ public void doHead(HttpServletRequest request, HttpServletResponse response, String[] pathElements) throws ServletException, IOException {
+ DescendingSiteResource.this.doHead(request, response, pathElements);
+ }
}
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|