Пример #1
0
  @Override
  public PrintWriter getWriter() throws IOException {
    if (_outputType == OutputType.STREAM) throw new IllegalStateException("STREAM");

    if (_outputType == OutputType.NONE) {
      /* get encoding from Content-Type header */
      String encoding = _characterEncoding;
      if (encoding == null) {
        encoding = MimeTypes.inferCharsetFromContentType(_contentType);
        if (encoding == null) encoding = StringUtil.__ISO_8859_1;
        setCharacterEncoding(encoding);
      }

      if (_writer != null && _writer.isFor(encoding)) _writer.reopen();
      else {
        if (StringUtil.__ISO_8859_1.equalsIgnoreCase(encoding))
          _writer = new ResponseWriter(new Iso88591HttpWriter(_out), encoding);
        else if (StringUtil.__UTF8.equalsIgnoreCase(encoding))
          _writer = new ResponseWriter(new Utf8HttpWriter(_out), encoding);
        else _writer = new ResponseWriter(new EncodingHttpWriter(_out, encoding), encoding);
      }

      // Set the output type at the end, because setCharacterEncoding() checks for it
      _outputType = OutputType.WRITER;
    }
    return _writer;
  }
Пример #2
0
  /**
   * Constructor that configures the cache of ResponseWriter objects.
   *
   * @param conf The Nutch configuration object.
   */
  public ResponseWriters(Configuration conf) {

    // get the cache and the cache key
    String cacheKey = ResponseWriter.class.getName();
    ObjectCache objectCache = ObjectCache.get(conf);
    this.responseWriters = (Map<String, ResponseWriter>) objectCache.getObject(cacheKey);

    // if already populated do nothing
    if (this.responseWriters == null) {

      try {

        // get the extension point and all ResponseWriter extensions
        ExtensionPoint point =
            PluginRepository.get(conf).getExtensionPoint(ResponseWriter.X_POINT_ID);
        if (point == null) {
          throw new RuntimeException(ResponseWriter.X_POINT_ID + " not found.");
        }

        // populate content type on the ResponseWriter classes, each response
        // writer can handle more than one response type
        Extension[] extensions = point.getExtensions();
        Map<String, ResponseWriter> writers = new HashMap<String, ResponseWriter>();
        for (int i = 0; i < extensions.length; i++) {
          Extension extension = extensions[i];
          ResponseWriter writer = (ResponseWriter) extension.getExtensionInstance();
          String[] responseTypes = extension.getAttribute("responseType").split(",");
          String contentType = extension.getAttribute("contentType");
          writer.setContentType(contentType);
          for (int k = 0; k < responseTypes.length; k++) {
            writers.put(responseTypes[k], writer);
          }
        }

        // set null object if no writers, otherwise set the writers
        if (writers == null) {
          objectCache.setObject(cacheKey, new HashMap<String, ResponseWriter>());
        } else {
          objectCache.setObject(cacheKey, writers);
        }
      } catch (PluginRuntimeException e) {
        throw new RuntimeException(e);
      }

      // set the response writers map
      this.responseWriters = (Map<String, ResponseWriter>) objectCache.getObject(cacheKey);
    }
  }
  protected void handle(HttpServletRequest hreq, HttpServletResponse hres, AnubisContext ctx)
      throws Exception {
    String pathInfo = hreq.getPathInfo();
    String path = hreq.getServletPath() + pathInfo;

    Project project = ctx.getProject();
    String urlPath = project.getUrl() + path;

    String themeName = pathInfo.substring(1, pathInfo.indexOf("/", 1));
    String resName = pathInfo.substring(pathInfo.indexOf("/", 1));

    ServletContext app = config.getServletContext();
    String mimeType = app.getMimeType(pathInfo);

    InputStream is = null;
    try {
      Theme theme = null;
      if (themeName.equals("system")) {
        theme = project.getSystemTheme();
      } else {
        theme = project.getThemes().get(themeName);
      }

      is = theme.getResource(resName);
      if (is != null) ResponseWriter.write(app, hreq, hres, mimeType, is);
    } catch (Exception e) {
      System.out.println("error theme resource " + e.getMessage());
    } finally {
      try {
        is.close();
      } catch (Exception ign) {;
      }
    }
  }
Пример #4
0
 public boolean closeIfAllContentWritten(long written) throws IOException {
   if (_contentLength >= 0 && written >= _contentLength) {
     switch (_outputType) {
       case WRITER:
         _writer.close();
         break;
       case STREAM:
         getOutputStream().close();
     }
     return true;
   }
   return false;
 }
Пример #5
0
  /** Renders the open tag for the text. */
  @Override
  public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
    ResponseWriter out = context.getResponseWriter();

    String id = component.getId();

    String accesskey;
    String dir;
    boolean disabled;
    String disabledClass;
    String enabledClass;
    String lang;

    String onblur;
    String onchange;
    String onclick;
    String ondblclick;
    String onfocus;

    String onkeydown;
    String onkeypress;
    String onkeyup;

    String onmousedown;
    String onmousemove;
    String onmouseout;
    String onmouseover;
    String onmouseup;

    String onselect;

    boolean readonly;
    String style;
    String styleClass;
    String tabindex;
    String title;
    Object value;

    if (component instanceof HtmlSelectOneMenu) {
      HtmlSelectOneMenu htmlComponent = (HtmlSelectOneMenu) component;

      accesskey = htmlComponent.getAccesskey();
      dir = htmlComponent.getDir();
      disabled = htmlComponent.isDisabled();
      disabledClass = htmlComponent.getDisabledClass();
      enabledClass = htmlComponent.getEnabledClass();
      lang = htmlComponent.getLang();

      onblur = htmlComponent.getOnblur();
      onchange = htmlComponent.getOnchange();
      onclick = htmlComponent.getOnclick();
      ondblclick = htmlComponent.getOndblclick();
      onfocus = htmlComponent.getOnfocus();

      onkeydown = htmlComponent.getOnkeydown();
      onkeypress = htmlComponent.getOnkeypress();
      onkeyup = htmlComponent.getOnkeyup();

      onmousedown = htmlComponent.getOnmousedown();
      onmousemove = htmlComponent.getOnmousemove();
      onmouseout = htmlComponent.getOnmouseout();
      onmouseover = htmlComponent.getOnmouseover();
      onmouseup = htmlComponent.getOnmouseup();

      onselect = htmlComponent.getOnselect();

      readonly = htmlComponent.isReadonly();
      style = htmlComponent.getStyle();
      styleClass = htmlComponent.getStyleClass();
      tabindex = htmlComponent.getTabindex();
      title = htmlComponent.getTitle();

      value = htmlComponent.getValue();
    } else {
      Map<String, Object> attrMap = component.getAttributes();

      accesskey = (String) attrMap.get("accesskey");
      dir = (String) attrMap.get("dir");
      disabled = Boolean.TRUE.equals(attrMap.get("disabled"));
      disabledClass = (String) attrMap.get("disabledClass");
      enabledClass = (String) attrMap.get("enabledClass");
      lang = (String) attrMap.get("lang");

      onblur = (String) attrMap.get("onblur");
      onchange = (String) attrMap.get("onchange");
      onclick = (String) attrMap.get("onclick");
      ondblclick = (String) attrMap.get("ondblclick");
      onfocus = (String) attrMap.get("onfocus");

      onkeydown = (String) attrMap.get("onkeydown");
      onkeypress = (String) attrMap.get("onkeypress");
      onkeyup = (String) attrMap.get("onkeyup");

      onmousedown = (String) attrMap.get("onmousedown");
      onmousemove = (String) attrMap.get("onmousemove");
      onmouseout = (String) attrMap.get("onmouseout");
      onmouseover = (String) attrMap.get("onmouseover");
      onmouseup = (String) attrMap.get("onmouseup");

      onselect = (String) attrMap.get("onselect");

      readonly = Boolean.TRUE.equals(attrMap.get("readonly"));
      style = (String) attrMap.get("style");
      styleClass = (String) attrMap.get("styleClass");
      tabindex = (String) attrMap.get("tabindex");
      title = (String) attrMap.get("title");

      value = attrMap.get("value");
    }

    UIViewRoot viewRoot = context.getViewRoot();

    out.startElement("select", component);

    if (style != null) out.writeAttribute("style", style, "style");

    if (styleClass != null) out.writeAttribute("class", styleClass, "class");

    String clientId = component.getClientId(context);
    out.writeAttribute("name", clientId, "name");

    if (id != null && !id.startsWith(UIViewRoot.UNIQUE_ID_PREFIX))
      out.writeAttribute("id", clientId, "id");

    if (disabled) out.writeAttribute("disabled", "disabled", "disabled");

    if (accesskey != null) out.writeAttribute("accesskey", accesskey, "accesskey");

    if (dir != null) out.writeAttribute("dir", dir, "dir");

    if (lang != null) out.writeAttribute("lang", lang, "lang");

    if (onblur != null) out.writeAttribute("onblur", onblur, "onblur");

    if (onchange != null) out.writeAttribute("onchange", onchange, "onchange");

    if (onclick != null) out.writeAttribute("onclick", onclick, "onclick");

    if (ondblclick != null) out.writeAttribute("ondblclick", ondblclick, "ondblclick");

    if (onfocus != null) out.writeAttribute("onfocus", onfocus, "onfocus");

    if (onkeydown != null) out.writeAttribute("onkeydown", onkeydown, "onkeydown");

    if (onkeypress != null) out.writeAttribute("onkeypress", onkeypress, "onkeypress");

    if (onkeyup != null) out.writeAttribute("onkeyup", onkeyup, "onkeyup");

    if (onmousedown != null) out.writeAttribute("onmousedown", onmousedown, "onmousedown");

    if (onmousemove != null) out.writeAttribute("onmousemove", onmousemove, "onmousemove");

    if (onmouseout != null) out.writeAttribute("onmouseout", onmouseout, "onmouseout");

    if (onmouseover != null) out.writeAttribute("onmouseover", onmouseover, "onmouseover");

    if (onmouseup != null) out.writeAttribute("onmouseup", onmouseup, "onmouseup");

    if (onselect != null) out.writeAttribute("onselect", onselect, "onselect");

    if (readonly) out.writeAttribute("readonly", "readonly", "readonly");

    if (tabindex != null) out.writeAttribute("tabindex", tabindex, "tabindex");

    if (title != null) out.writeAttribute("title", title, "title");

    out.writeAttribute("size", "1", "size");

    out.write("\n");

    encodeOneChildren(out, context, component, value, enabledClass, disabledClass);

    out.endElement("select");
    out.write("\n");

    for (UIComponent child : component.getChildren()) {
      if (child instanceof UIComponent) child.encodeAll(context);
    }
  }
Пример #6
0
  public void doFilter(
      ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
      throws IOException, ServletException {
    HttpServletRequest hreq = (HttpServletRequest) servletRequest;
    HttpServletResponse hres = (HttpServletResponse) servletResponse;
    AnubisContext.setContext(new WebAnubisContext(config.getServletContext(), hreq, hres));
    try {
      ServletContext app = config.getServletContext();
      String pattern = buildIgnoredPattern();

      if (!hreq.getServletPath().matches(pattern)) {
        // run the CMS file

        Project project = AnubisContext.getCurrentContext().getProject();
        Map params = CmsWebUtil.buildRequestParams(hreq);

        String mimeType = app.getMimeType(hreq.getServletPath());
        String ext = CmsWebConstants.PAGE_FILE_EXT;
        if (mimeType == null) {
          mimeType = "text/html";
        } else {
          // if mimetype is not null then automatically we consider it as media.
          ext = CmsWebConstants.MEDIA_FILE_EXT;
        }

        String spath = hreq.getServletPath();
        if (spath.equals("/")) {
          Folder folder = project.getFileManager().getFolder("/");
          File ff = ProjectUtils.findFirstVisibleFile(folder);
          if (ff != null) spath = ff.getPath();
        } else {
          if (spath.endsWith("/")) spath = spath.substring(0, spath.length() - 1);
        }

        String filename = spath + ext;

        FileInstance file = null;
        try {
          file = project.getFileManager().getFile(filename, params);
        } catch (com.rameses.anubis.FileNotFoundException fe) {
          hres.setStatus(HttpServletResponse.SC_NOT_FOUND);
          file = project.getFileManager().getFile("/404.pg", params);
          ResponseWriter.write(app, hreq, hres, mimeType, file.getContent());
          return;
        } catch (Exception e) {
          e.printStackTrace();
          throw new ServletException(e.getMessage());
        }

        if (file.getHref() != null) {
          hres.sendRedirect(file.getHref());
          return;
        }

        SessionContext ctx = AnubisContext.getCurrentContext().getSession();

        // set authenicated as true if there is sessionid
        boolean allow_access = true;
        if (file.isSecured()) {
          if (!ctx.isLoggedIn()) allow_access = false;
          else if (!ctx.checkFilePermission(file)) allow_access = false;
        }

        if (!allow_access) {
          String path = CmsWebConstants.LOGIN_PAGE_PATH;
          String requestPath = hreq.getRequestURI();
          String qry = hreq.getQueryString();
          if (qry != null && qry.trim().length() > 0) {
            requestPath += "?" + qry;
          }
          hres.sendRedirect(path + "?target=" + URLEncoder.encode(requestPath));
        } else {
          InputStream is = file.getContent();
          ResponseWriter.write(app, hreq, hres, mimeType, is);
        }
      } else {
        filterChain.doFilter(servletRequest, servletResponse);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      AnubisContext.removeContext();
    }
  }