Subversion Repositories general

Rev

Rev 1249 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package ak.photoalbum.webapp;

import java.io.FileNotFoundException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.log4j.Logger;
import ak.photoalbum.logic.Logic;
import ak.photoalbum.util.TimestampRecipient;

public final class ImageAction
  extends BaseAction
{
  private static final Logger logger = Logger.getLogger(ImageAction.class);

  public ActionForward executeAction(ActionMapping mapping, ActionForm form,
     HttpServletRequest request, HttpServletResponse response)
    throws Exception
  {
    PathForm               theForm            = (PathForm)form;
    String                 branch             = theForm.getBranch();
    String                 path               = theForm.getPath();
    Logic                  logic              = Logic.getLogic();
    HttpTimestampRecipient timestampRecipient = new HttpTimestampRecipient(response);
    long                   ifModifiedSince;
    boolean                fileModified       = true;

    try {
      ifModifiedSince = request.getDateHeader("If-Modified-Since");
    }
    catch(Exception ex) {
      ifModifiedSince = -1;
    }

    logger.info("get image " + mapping.getParameter() + " for " + path);

    if("dir".equals(mapping.getParameter())) {
      response.setContentType(logic.getThumbnailMime(branch));
      fileModified = logic.writeDir(branch, path, ifModifiedSince,
        response.getOutputStream(), timestampRecipient);
    }
    else if("small".equals(mapping.getParameter())) {
      response.setContentType(logic.getThumbnailMime(branch));
      fileModified = logic.writeSmall(branch, path, ifModifiedSince,
        response.getOutputStream(), timestampRecipient);
    }
    else if("medium".equals(mapping.getParameter())) {
      response.setContentType(logic.getThumbnailMime(branch));
      fileModified = logic.writeMedium(branch, path, ifModifiedSince,
        response.getOutputStream(), timestampRecipient);
    }
    else if("origin".equals(mapping.getParameter())) {
      response.setContentType(logic.getOriginMime(branch, path));
      fileModified = logic.writeOrigin(branch, path, ifModifiedSince,
        response.getOutputStream(), timestampRecipient);
    }

    // if file was modified comapring to "If-Modified-Since" header, then it's already written
    // into to response; otherwise send 304 code
    if(!fileModified) {
      response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
    }

    return null;
  }

  private class HttpTimestampRecipient
    implements TimestampRecipient
  {
    private HttpServletResponse response;

    public HttpTimestampRecipient(HttpServletResponse response)
    {
      this.response = response;
    }

    public void setTimestamp(long timestamp)
    {
      response.setDateHeader("Last-Modified", timestamp);
    }
  }
}