/**
   * Logs a message to the error log.
   *
   * @param log the error log to write the message.
   * @param message the message to write
   * @param e the exception to write
   */
  public void log(
      String message,
      Throwable e,
      HttpServletRequest request,
      HttpServletResponse response,
      ServletContext application)
      throws IOException {
    WriteStream logStream = getLogStream();

    if (logStream == null) return;

    Throwable t = e;
    while (t != null) {
      e = t;

      if (e instanceof ServletException) t = ((ServletException) e).getRootCause();
      else if (e instanceof ExceptionWrapper) t = ((ExceptionWrapper) e).getRootCause();
      else t = null;
    }

    CharBuffer cb = CharBuffer.allocate();

    QDate.formatLocal(cb, CurrentTime.getCurrentTime(), "[%Y/%m/%d %H:%M:%S] ");

    cb.append(message);

    logStream.log(cb.close());

    if (e != null && !(e instanceof CompileException)) logStream.log(e);

    logStream.flush();
  }
  private void checkMetaEncoding(Element elt) {
    String http = elt.getAttribute("http-equiv");
    String content = elt.getAttribute("content");
    if (http.equals("") || content.equals("") || !http.equalsIgnoreCase("content-type")) return;

    CharCursor cursor = new StringCharCursor(content);
    charsetScanner.scan(cursor);
    charsetScanner.skip(cursor);
    CharBuffer buf = CharBuffer.allocate();
    while (cursor.current() != cursor.DONE) {
      buf.clear();
      charsetScanner.scan(cursor, buf);
      if (buf.toString().equalsIgnoreCase("charset")) {
        charsetScanner.skip(cursor);
        buf.clear();
        charsetScanner.scan(cursor, buf);
        if (buf.length() > 0) {
          try {
            is.setEncoding(buf.close());
          } catch (IOException e) {
          }
          return;
        }
      }
    }
  }
Beispiel #3
0
  /** Escapes the XML string. */
  public static String escapeXml(String string) {
    if (string == null) return "";

    CharBuffer cb = CharBuffer.allocate();

    for (int i = 0; i < string.length(); i++) {
      int ch = string.charAt(i);

      switch (ch) {
        case '<':
          cb.append("&lt;");
          break;
        case '>':
          cb.append("&gt;");
          break;
        case '&':
          cb.append("&amp;");
          break;
        case '\'':
          cb.append("&#039;");
          break;
        case '"':
          cb.append("&#034;");
          break;
        default:
          cb.append((char) ch);
          break;
      }
    }

    return cb.close();
  }
  private String digestToBase64(byte[] digest, int len) {
    CharBuffer cb = CharBuffer.allocate();

    Base64.encode(cb, digest, 0, len);

    return cb.close();
  }
Beispiel #5
0
  /** Initialize the server. */
  private void serverInit(CauchoRequest req) throws ServletException {
    if (_urlPrefix != null) return;

    WebApp app = (WebApp) getServletContext();

    // calculate the URL prefix
    _servletId = req.getServletPath();

    CharBuffer cb = CharBuffer.allocate();

    if (!"default".equals(app.getAdmin().getHost().getName())
        && !"".equals(app.getAdmin().getHost().getName())) {
      String hostName = app.getAdmin().getHost().getURL();

      cb.append(hostName);
      cb.append(app.getContextPath());
      cb.append(_servletId);
    } else {
      cb.append(req.getScheme());
      cb.append("://");
      cb.append(req.getServerName());
      cb.append(":");
      cb.append(req.getServerPort());
      cb.append(app.getContextPath());
      cb.append(_servletId);
    }

    _urlPrefix = cb.close();

    initProtocol();
  }
 /**
  * The location of a javadoc generated api, can be a url, required.
  *
  * <p>Examples:
  *
  * <ul>
  *   <li>http://java.sun.com/j2se/1.4.2/docs/api
  *   <li>file://usr/local/java/axis_1-1/docs/apiDocs
  *   <li>resin/
  * </ul>
  */
 public void setLocation(String location) {
   if (!location.endsWith("/")) {
     CharBuffer cb = CharBuffer.allocate();
     cb.append(location);
     cb.append('/');
     _location = cb.close();
   } else _location = location;
 }
  /**
   * A location href, relative to the web-app root, appropriately rewritten to handle remote
   * locations and locations that are local absolute.
   */
  String getLocationHref(String file) {
    CharBuffer cb = CharBuffer.allocate();

    if (_isLocalAbsolute) {
      cb.append(_id);
      cb.append('/');
    } else {
      cb.append(_location);
    }

    cb.append(file);

    return cb.close();
  }
  /** Returns a printable version. */
  public String toString() {
    CharBuffer cb = new CharBuffer();
    cb.append("AttributesImpl[");
    for (int i = 0; i < _size; i++) {
      cb.append(" ");
      cb.append(_names[i]);
      cb.append("=\"");
      cb.append(_values[i]);
      cb.append("\"");
    }
    cb.append("]");

    return cb.close();
  }
Beispiel #9
0
  /** Joins the array. */
  public static String join(String[] array, String sep) {
    if (array == null) return "";

    if (sep == null) sep = "";

    CharBuffer result = CharBuffer.allocate();

    for (int i = 0; i < array.length; i++) {
      if (i != 0) result.append(sep);

      result.append(array[i]);
    }

    return result.close();
  }
  /**
   * Logs an error.
   *
   * @param message the error message
   * @param request the servlet request
   * @param response the servlet response
   * @param application the servlet context
   */
  public void log(
      String message,
      HttpServletRequest request,
      HttpServletResponse response,
      ServletContext application)
      throws IOException {
    WriteStream logStream = getLogStream();

    if (logStream == null) return;

    CharBuffer cb = CharBuffer.allocate();

    QDate.formatLocal(cb, CurrentTime.getCurrentTime(), "[%Y/%m/%d %H:%M:%S] ");

    cb.append(message);

    logStream.log(cb.close());

    logStream.flush();
  }
Beispiel #11
0
  /** Replaces substrings. */
  public static String replace(String input, String before, String after) {
    if (input == null) return "";

    if (before == null || before.equals("")) return input;

    if (after == null) after = "";

    CharBuffer result = CharBuffer.allocate();

    int head = 0;
    int next;
    while ((next = input.indexOf(before, head)) >= 0) {
      result.append(input.substring(head, next));

      result.append(after);

      head = next + before.length();
    }

    result.append(input.substring(head));

    return result.close();
  }
Beispiel #12
0
  /**
   * Walk down the path starting from the portion immediately following the scheme. i.e. schemeWalk
   * is responsible for parsing the host and port from the URL.
   *
   * @param userPath the user's passed in path
   * @param attributes the attributes for the new path
   * @param uri the normalized full uri
   * @param offset offset into the uri to start processing, i.e. after the scheme.
   * @return the looked-up path.
   */
  @Override
  public Path schemeWalk(String userPath, Map<String, Object> attributes, String uri, int offset) {
    int length = uri.length();

    if (length < 2 + offset || uri.charAt(offset) != '/' || uri.charAt(offset + 1) != '/')
      throw new RuntimeException(L.l("bad scheme in `{0}'", uri));

    CharBuffer buf = CharBuffer.allocate();
    int i = 2 + offset;
    int ch = 0;
    boolean isInBrace = false;

    for (;
        i < length && ((ch = uri.charAt(i)) != ':' || isInBrace) && ch != '/' && ch != '?';
        i++) {
      buf.append((char) ch);

      if (ch == '[') isInBrace = true;
      else if (ch == ']') isInBrace = false;
    }

    String host = buf.close();
    if (host.length() == 0) throw new RuntimeException(L.l("bad host in `{0}'", uri));

    int port = 0;
    if (ch == ':') {
      for (i++; i < length && (ch = uri.charAt(i)) >= '0' && ch <= '9'; i++) {
        port = 10 * port + uri.charAt(i) - '0';
      }
    }

    if (port == 0) port = 80;

    HttpPath root = create(host, port);

    return root.fsWalk(userPath, attributes, uri.substring(i));
  }