Exemple #1
0
  /** Handles requests for user uploaded resources. */
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    Weblog weblog;
    // String ctx = request.getContextPath();
    // String servlet = request.getServletPath();
    // String reqURI = request.getRequestURI();

    WeblogResourceRequest resourceRequest;
    try {
      // parse the incoming request and extract the relevant data
      resourceRequest = new WeblogResourceRequest(request);

      weblog = resourceRequest.getWeblog();
      if (weblog == null) {
        throw new WebloggerException(
            "unable to lookup weblog: " + resourceRequest.getWeblogHandle());
      }

    } catch (Exception e) {
      // invalid resource request or weblog doesn't exist
      if (!response.isCommitted()) {
        response.reset();
      }
      log.debug("error creating weblog resource request", e);
      response.sendError(HttpServletResponse.SC_NOT_FOUND);
      return;
    }

    log.debug("Resource requested [" + resourceRequest.getResourcePath() + "]");

    long resourceLastMod = 0;
    InputStream resourceStream = null;

    // first see if resource comes from weblog's shared theme
    try {
      WeblogTheme weblogTheme = weblog.getTheme();
      if (weblogTheme != null) {
        ThemeResource resource = weblogTheme.getResource(resourceRequest.getResourcePath());
        if (resource != null) {
          resourceLastMod = resource.getLastModified();
          resourceStream = resource.getInputStream();
        }
      }
    } catch (Exception ex) {
      // hmmm, some kind of error getting theme. that's an error.
      if (!response.isCommitted()) {
        response.reset();
      }
      response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
      return;
    }

    // if not from theme then see if resource is in weblog's upload dir
    if (resourceStream == null) {
      try {
        MediaFileManager mmgr = WebloggerFactory.getWeblogger().getMediaFileManager();
        MediaFile mf = mmgr.getMediaFileByOriginalPath(weblog, resourceRequest.getResourcePath());
        resourceLastMod = mf.getLastModified();
        resourceStream = mf.getInputStream();

      } catch (Exception ex) {
        // still not found? then we don't have it, 404.
        if (!response.isCommitted()) {
          response.reset();
        }
        log.debug("Unable to get resource", ex);
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
      }
    }

    // Respond with 304 Not Modified if it is not modified.
    if (ModDateHeaderUtil.respondIfNotModified(
        request, response, resourceLastMod, resourceRequest.getDeviceType())) {
      return;
    } else {
      // set last-modified date
      ModDateHeaderUtil.setLastModifiedHeader(
          response, resourceLastMod, resourceRequest.getDeviceType());
    }

    // set the content type based on whatever is in our web.xml mime defs
    response.setContentType(this.context.getMimeType(resourceRequest.getResourcePath()));

    OutputStream out;
    try {
      // ok, lets serve up the file
      byte[] buf = new byte[RollerConstants.EIGHT_KB_IN_BYTES];
      int length;
      out = response.getOutputStream();
      while ((length = resourceStream.read(buf)) > 0) {
        out.write(buf, 0, length);
      }

      // close output stream
      out.close();

    } catch (Exception ex) {
      if (!response.isCommitted()) {
        response.reset();
      }
      response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    } finally {
      // make sure stream to resource file is closed
      resourceStream.close();
    }
  }
Exemple #2
0
  /** Handle GET requests for weblog pages. */
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    log.debug("Entering");

    Weblog weblog = null;

    WeblogRequest weblogRequest = null;
    try {
      weblogRequest = new WeblogRequest(request);

      // now make sure the specified weblog really exists
      weblog = weblogRequest.getWeblog();
      if (weblog == null) {
        throw new WebloggerException("Unable to lookup weblog: " + weblogRequest.getWeblogHandle());
      }

    } catch (Exception e) {
      // invalid rsd request format or weblog doesn't exist
      log.debug("error creating weblog request", e);
      response.sendError(HttpServletResponse.SC_NOT_FOUND);
      return;
    }

    // Respond with 304 Not Modified if it is not modified.
    long lastModified = System.currentTimeMillis();
    if (weblog.getLastModified() != null) {
      lastModified = weblog.getLastModified().getTime();
    }
    if (ModDateHeaderUtil.respondIfNotModified(request, response, lastModified)) {
      return;
    }

    // set last-modified date
    ModDateHeaderUtil.setLastModifiedHeader(response, lastModified);

    // set the content type
    response.setContentType("application/rsd+xml; charset=utf-8");

    // populate the model
    HashMap model = new HashMap();
    model.put("website", weblog);
    model.put("absBaseURL", WebloggerRuntimeConfig.getAbsoluteContextURL());

    // lookup Renderer we are going to use
    Renderer renderer = null;
    try {
      log.debug("Looking up renderer");
      Template template = new StaticTemplate("rsd.vm", "velocity");
      renderer = RendererManager.getRenderer(template, DeviceType.standard);
    } catch (Exception e) {
      // nobody wants to render my content :(
      log.error("Couldn't find renderer for rsd template", e);

      if (!response.isCommitted()) response.reset();
      response.sendError(HttpServletResponse.SC_NOT_FOUND);
      return;
    }

    // render content
    CachedContent rendererOutput = new CachedContent(4096);
    try {
      log.debug("Doing rendering");
      renderer.render(model, rendererOutput.getCachedWriter());

      // flush rendered output and close
      rendererOutput.flush();
      rendererOutput.close();
    } catch (Exception e) {
      // bummer, error during rendering
      log.error("Error during rendering for rsd template", e);

      if (!response.isCommitted()) response.reset();
      response.sendError(HttpServletResponse.SC_NOT_FOUND);
      return;
    }

    // post rendering process

    // flush rendered content to response
    log.debug("Flushing response output");
    response.setContentLength(rendererOutput.getContent().length);
    response.getOutputStream().write(rendererOutput.getContent());

    log.debug("Exiting");
  }