예제 #1
0
 public void destroy() {
   for (Iterator it = _aues.values().iterator(); it.hasNext(); ) {
     final AuExtension aue = (AuExtension) it.next();
     try {
       aue.destroy();
     } catch (Throwable ex) {
       log.warningBriefly("Unable to stop " + aue, ex);
     }
   }
 }
예제 #2
0
  // called by Provider
  InputStream getResourceAsStream(HttpServletRequest request, String path, boolean locate)
      throws IOException, ServletException {
    if (locate)
      path = Servlets.locate(_webctx.getServletContext(), request, path, _webctx.getLocator());

    if (_cache.getCheckPeriod() >= 0) {
      // Due to Web server might cache the result, we use URL if possible
      try {
        URL url = _webctx.getResource(path);
        if (url != null) return url.openStream();
      } catch (Throwable ex) {
        log.warningBriefly("Unable to read from URL: " + path, ex);
      }
    }

    // Note: _webctx will handle the renaming for debugJS (.src.js)
    return _webctx.getResourceAsStream(path);
  }
예제 #3
0
  /**
   * Adds an AU extension and associates it with the specified prefix.
   *
   * <p>If there was an AU extension associated with the same name, the the old AU extension will be
   * replaced.
   *
   * <p>If you want to add an Au extension, even before DHtmlUpdateServlet is started, use {@link
   * #addAuExtension(WebApp, String, AuExtension)} instead.
   *
   * @param prefix the prefix. It must start with "/", but it cannot be "/" nor "/web" (which are
   *     reserved).
   * @param extension the AU extension (never null).
   * @return the previous AU extension associated with the specified prefix, or null if the prefix
   *     was not associated before.
   * @see #addAuExtension(WebApp,String,AuExtension)
   * @since 5.0.0
   */
  public AuExtension addAuExtension(String prefix, AuExtension extension) throws ServletException {
    checkAuExtension(prefix, extension);

    if (_aues.get(prefix) == extension) // speed up to avoid sync
    return extension; // nothing changed

    extension.init(this);

    // To avoid using sync in doGet(), we make a copy here
    final AuExtension old;
    synchronized (this) {
      final Map ps = new HashMap(_aues);
      old = (AuExtension) ps.put(prefix, extension);
      _aues = ps;
    }
    if (old != null)
      try {
        old.destroy();
      } catch (Throwable ex) {
        log.warningBriefly("Unable to stop " + old, ex);
      }
    return old;
  }
예제 #4
0
  /**
   * Process asynchronous update requests from the client.
   *
   * @since 3.0.0
   */
  protected void process(Session sess, HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    final String errClient = request.getHeader("ZK-Error-Report");
    if (errClient != null)
      if (log.debugable())
        log.debug("Error found at client: " + errClient + "\n" + Servlets.getDetail(request));

    // parse desktop ID
    final WebApp wapp = sess.getWebApp();
    final WebAppCtrl wappc = (WebAppCtrl) wapp;
    final AuDecoder audec = getAuDecoder(wapp);
    final String dtid = audec.getDesktopId(request);
    if (dtid == null) {
      // Bug 1929139: incomplete request (IE only)
      if (log.debugable()) {
        final String msg = "Incomplete request\n" + Servlets.getDetail(request);
        log.debug(msg);
      }

      response.sendError(467, "Incomplete request");
      return;
    }

    Desktop desktop = getDesktop(sess, dtid);
    if (desktop == null) {
      final String cmdId = audec.getFirstCommand(request);
      if (!"rmDesktop".equals(cmdId))
        desktop = recoverDesktop(sess, request, response, wappc, dtid);

      if (desktop == null) {
        response.setIntHeader("ZK-Error", response.SC_GONE); // denote timeout
        sessionTimeout(request, response, wapp, dtid);
        return;
      }
    }
    WebManager.setDesktop(request, desktop);
    // reason: a new page might be created (such as include)

    final String sid = request.getHeader("ZK-SID");
    if (sid != null) // Some client might not have ZK-SID
    response.setHeader("ZK-SID", sid);

    // parse commands
    final Configuration config = wapp.getConfiguration();
    final List aureqs;
    boolean keepAlive = false;
    try {
      final boolean timerKeepAlive = config.isTimerKeepAlive();
      aureqs = audec.decode(request, desktop);
      for (Iterator it = aureqs.iterator(); it.hasNext(); ) {
        final String cmdId = ((AuRequest) it.next()).getCommand();
        keepAlive = !(!timerKeepAlive && Events.ON_TIMER.equals(cmdId)) && !"dummy".equals(cmdId);
        // dummy is used for PollingServerPush for piggyback
        if (keepAlive) break; // done
      }
    } catch (Throwable ex) {
      log.warningBriefly(ex);
      responseError(request, response, Exceptions.getMessage(ex));
      return;
    }

    if (aureqs.isEmpty()) {
      final String errmsg = "Illegal request: cmd required";
      log.debug(errmsg);
      responseError(request, response, errmsg);
      return;
    }

    ((SessionCtrl) sess).notifyClientRequest(keepAlive);

    //		if (log.debugable()) log.debug("AU request: "+aureqs);
    final DesktopCtrl desktopCtrl = (DesktopCtrl) desktop;
    final Execution exec = new ExecutionImpl(getServletContext(), request, response, desktop, null);
    if (sid != null) ((ExecutionCtrl) exec).setRequestId(sid);

    final AuWriter out = AuWriters.newInstance();
    out.setCompress(_compress);
    out.open(
        request,
        response,
        desktop.getDevice().isSupported(Device.RESEND)
            ? getProcessTimeout(config.getResendDelay())
            : 0);
    // Note: getResendDelay() might return nonpositive
    try {
      wappc.getUiEngine().execUpdate(exec, aureqs, out);
    } catch (RequestOutOfSequenceException ex) {
      log.warning(ex.getMessage());
      response.setHeader("ZK-SID", sid);
      response.setIntHeader("ZK-Error", AuResponse.SC_OUT_OF_SEQUENCE);
    }
    out.close(request, response);
  }
예제 #5
0
  // Servlet//
  public void init() throws ServletException {
    final ServletConfig config = getServletConfig();
    final ServletContext ctx = getServletContext();
    ctx.setAttribute(ATTR_UPDATE_SERVLET, this);

    final WebManager webman = WebManager.getWebManager(ctx);
    String param = config.getInitParameter("compress");
    _compress = param == null || param.length() == 0 || "true".equals(param);
    if (!_compress) webman.getClassWebResource().setCompress(null); // disable all

    // Copies au extensions defined before DHtmlUpdateServlet is started
    final WebApp wapp = webman.getWebApp();
    final Map aues = (Map) wapp.getAttribute(ATTR_AU_PROCESSORS);
    if (aues != null) {
      for (Iterator it = aues.entrySet().iterator(); it.hasNext(); ) {
        final Map.Entry me = (Map.Entry) it.next();
        addAuExtension((String) me.getKey(), (AuExtension) me.getValue());
      }
      wapp.removeAttribute(ATTR_AU_PROCESSORS);
    }

    // ZK 5: extension defined in init-param has the higher priority
    for (int j = 0; ; ++j) {
      param = config.getInitParameter("extension" + j);
      if (param == null) {
        param = config.getInitParameter("processor" + j); // backward compatible
        if (param == null) break;
      }
      final int k = param.indexOf('=');
      if (k < 0) {
        log.warning("Ignore init-param: illegal format, " + param);
        continue;
      }

      final String prefix = param.substring(0, k).trim();
      final String clsnm = param.substring(k + 1).trim();
      try {
        addAuExtension(prefix, (AuExtension) Classes.newInstanceByThread(clsnm));
      } catch (ClassNotFoundException ex) {
        log.warning("Ignore init-param: class not found, " + clsnm);
      } catch (ClassCastException ex) {
        log.warning("Ignore: " + clsnm + " not implement " + AuExtension.class);
      } catch (Throwable ex) {
        log.warning("Ignore init-param: failed to add an AU extension, " + param, ex);
      }
    }

    if (getAuExtension("/upload") == null) {
      try {
        addAuExtension("/upload", new AuUploader());
      } catch (Throwable ex) {
        final String msg = "Make sure commons-fileupload.jar is installed.";
        log.warningBriefly("Failed to configure fileupload. " + msg, ex);

        // still add /upload to generate exception when fileupload is used
        addAuExtension(
            "/upload",
            new AuExtension() {
              public void init(DHtmlUpdateServlet servlet) {}

              public void destroy() {}

              public void service(
                  HttpServletRequest request, HttpServletResponse response, String pi)
                  throws ServletException, IOException {
                if (Sessions.getCurrent(false) != null)
                  throw new ServletException("Failed to upload. " + msg);
              }
            });
      }
    }

    if (getAuExtension("/view") == null) addAuExtension("/view", new AuDynaMediar());
  }
예제 #6
0
  /**
   * Loads, parses and returns the resource of the specified URI, or null if not found. The parser
   * is defined by the loader defined in {@link ResourceCache}.
   *
   * @param cache the resource cache. Note: its loader must extend from {@link ResourceLoader}.
   * @param path the URI path
   * @param extra the extra parameter that will be passed to {@link
   *     ResourceLoader#parse(String,File,Object)} and {@link
   *     ResourceLoader#parse(String,URL,Object)}
   */
  public static final <V> V get(
      ResourceCache<V> cache, ServletContext ctx, String path, Object extra) {
    // 20050905: Tom Yeh
    // We don't need to handle the default name if user specifies only a dir
    // because it is handled by the container directly
    // And, web  developer has to specify <welcome-file> in web.xml
    URL url = null;
    if (path == null || path.length() == 0) path = "/";
    else if (path.charAt(0) != '/') {
      if (path.indexOf("://") > 0) {
        try {
          url = new URL(path);
        } catch (java.net.MalformedURLException ex) {
          throw new SystemException(ex);
        }
      } else path = '/' + path;
    }

    if (url == null) {
      if (path.startsWith("/~")) {
        final ServletContext ctx0 = ctx;
        final String path0 = path;
        final int j = path.indexOf('/', 2);
        final String ctxpath;
        if (j >= 0) {
          ctxpath = "/" + path.substring(2, j);
          path = path.substring(j);
        } else {
          ctxpath = "/" + path.substring(2);
          path = "/";
        }

        final ExtendletContext extctx = Servlets.getExtendletContext(ctx, ctxpath.substring(1));
        if (extctx != null) {
          url = extctx.getResource(path);
          //					if (log.debugable()) log.debug("Resolving "+path0+" to "+url);
          if (url == null) return null;
          try {
            return cache.get(new ResourceInfo(path, url, extra));
          } catch (Throwable ex) {
            final IOException ioex = getIOException(ex);
            if (ioex == null) throw SystemException.Aide.wrap(ex);
            log.warningBriefly("Unable to load " + url, ioex);
          }
          return null;
        }

        ctx = ctx.getContext(ctxpath);
        if (ctx == null) { // failed
          //					if (log.debugable()) log.debug("Context not found: "+ctxpath);
          ctx = ctx0;
          path = path0; // restore
        }
      }

      final String flnm = ctx.getRealPath(path);
      if (flnm != null) {
        try {
          return cache.get(new ResourceInfo(path, new File(flnm), extra));
          // it is loader's job to check the existence
        } catch (Throwable ex) {
          final IOException ioex = getIOException(ex);
          if (ioex == null) throw SystemException.Aide.wrap(ex);
          log.warningBriefly("Unable to load " + flnm, ioex);
        }
        return null;
      }
    }

    // try url because some server uses JAR format
    try {
      if (url == null) url = ctx.getResource(path);
      if (url != null) return cache.get(new ResourceInfo(path, url, extra));
    } catch (Throwable ex) {
      final IOException ioex = getIOException(ex);
      if (ioex == null) throw SystemException.Aide.wrap(ex);
      log.warningBriefly("Unable to load " + path, ioex);
    }
    return null;
  }