示例#1
0
 /**
  * Writes the given value only if it is serializable. If not, null is written.
  *
  * @since 5.0.7
  */
 public static void smartWrite(ObjectOutputStream s, Object val) throws IOException {
   final boolean bser =
       val instanceof java.io.Serializable || val instanceof java.io.Externalizable;
   s.writeObject(bser ? val : null);
   if (!bser && val != null && logio.debugable())
     logio.debug("Skip not-serializable object: " + val);
 }
 /**
  * rollback the current session.
  *
  * @param exec the exection to clean up.
  * @param ex the StaleObjectStateException being thrown (and not handled) during the execution
  */
 private void rollback(Execution exec, Throwable ex) {
   try {
     if (HibernateUtil.currentSession().getTransaction().isActive()) {
       log.debug("Trying to rollback database transaction after exception:" + ex);
       HibernateUtil.currentSession().getTransaction().rollback();
     }
   } catch (Throwable rbEx) {
     log.error("Could not rollback transaction after exception! Original Exception:\n" + ex, rbEx);
   }
 }
示例#3
0
 public void addRequests(Collection requests) {
   for (Iterator it = requests.iterator(); it.hasNext(); ) {
     final AuRequest request = (AuRequest) it.next();
     try {
       request.activate();
       if (!isObsolete(request)) addRequest(request);
     } catch (ComponentNotFoundException ex) { // ignore it
       // ignore it since a long request might remove a timer
       // while clients already queues onTimer generated by the timer
       if (log.debugable()) log.debug("Ignore request: " + ex.getMessage());
     }
   }
 }
示例#4
0
 /** Writes only serializable elements of the specified collection. */
 public static <T> void smartWrite(ObjectOutputStream s, Collection<T> col) throws IOException {
   if (col != null) {
     final boolean debug = logio.debugable();
     for (T val : col) {
       if ((val instanceof Serializable) || (val instanceof Externalizable)) {
         try {
           s.writeObject(val);
         } catch (java.io.NotSerializableException ex) {
           logio.error("Unable to serialize item: " + val);
           throw ex;
         }
       } else if (val != null && debug) {
         logio.debug("Skip not-serializable item: " + val);
       }
     }
   }
   s.writeObject(null);
 }
示例#5
0
 /**
  * Writes only serializable elements of the specified array.
  *
  * <p>To read back, use {@link #smartRead(ObjectInputStream, Collection)}.
  *
  * @since 3.0.0
  */
 public static <T> void smartWrite(ObjectOutputStream s, T[] ary) throws IOException {
   if (ary != null) {
     final boolean debug = logio.debugable();
     for (int j = 0; j < ary.length; ++j) {
       final T val = ary[j];
       if ((val instanceof Serializable) || (val instanceof Externalizable)) {
         try {
           s.writeObject(val);
         } catch (java.io.NotSerializableException ex) {
           logio.error("Unable to serialize item: " + val);
           throw ex;
         }
       } else if (val != null && debug) {
         logio.debug("Skip not-serializable item: " + val);
       }
     }
   }
   s.writeObject(null);
 }
示例#6
0
  /**
   * Clones the specified object. Use clone() if Cloeable. Otherwise, try to serialize/deserialize
   * it by use of MarshalledObject.
   *
   * <p>If o is null, null is returned.
   *
   * @exception SystemException if failed to clone
   */
  public static final Object clone(Object o) {
    if (o == null) return o;

    try {
      final Class<?> kls = o.getClass();
      if (kls.isArray()) return ArraysX.clone(o);

      if (o instanceof Cloneable) {
        try {
          return kls.getMethod("clone").invoke(o);
        } catch (NoSuchMethodException ex) {
          if (log.debugable()) log.debug("No clone() for " + kls);
        }
      }

      // :TODO: MarshalledObject is said with very bad performance, change it
      // if exists other good deep clone method.
      return new MarshalledObject<Object>(o).get();
    } catch (Exception ex) {
      throw SystemException.Aide.wrap(ex);
    }
  }
示例#7
0
 /**
  * Writes only serializable entries of the specified map. Non-serializable attributes are ignored.
  */
 public static <K, V> void smartWrite(ObjectOutputStream s, Map<K, V> map) throws IOException {
   if (map != null) {
     final boolean debug = logio.debugable();
     for (Map.Entry<K, V> me : map.entrySet()) {
       final K nm = me.getKey();
       final V val = me.getValue();
       if (((nm instanceof Serializable) || (nm instanceof Externalizable))
           && (val == null || (val instanceof Serializable) || (val instanceof Externalizable))) {
         try {
           s.writeObject(nm);
           s.writeObject(val);
         } catch (java.io.NotSerializableException ex) {
           logio.error("Unable to serialize entry: " + nm + '=' + val);
           throw ex;
         }
       } else if (nm != null && debug) {
         logio.debug("Skip not-serializable entry: " + nm + '=' + val);
       }
     }
   }
   s.writeObject(null); // denote end-of-map
 }
 // -- ExecutionCleanup --//
 public void cleanup(Execution exec, Execution parent, List errs) {
   if (parent == null) { // the root execution of a servlet request
     try {
       if (errs == null || errs.isEmpty()) {
         // Commit and cleanup
         log.debug("Committing the database transaction: " + exec);
         HibernateUtil.currentSession().getTransaction().commit();
       } else {
         final Throwable ex = (Throwable) errs.get(0);
         if (ex instanceof StaleObjectStateException) {
           // default implementation does not do any optimistic concurrency
           // control; it simply rollback the transaction.
           handleStaleObjectStateException(exec, (StaleObjectStateException) ex);
         } else {
           // default implementation log the stacktrace and then rollback
           // the transaction.
           handleOtherException(exec, ex);
         }
       }
     } finally {
       HibernateUtil.closeSession(); // always close it
     }
   }
 }
 // -- ExecutionInit --//
 public void init(Execution exec, Execution parent) {
   if (parent == null) { // the root execution of a servlet request
     log.debug("Starting a database transaction: " + exec);
     HibernateUtil.currentSession().beginTransaction();
   }
 }
示例#10
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);
  }
示例#11
0
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    final String pi = Https.getThisPathInfo(request);
    //		if (log.finerable()) log.finer("Path info: "+pi);

    final ServletContext ctx = getServletContext();
    final boolean withpi = pi != null && pi.length() != 0;
    if (withpi && pi.startsWith(ClassWebResource.PATH_PREFIX)) {
      // use HttpSession to avoid loading SerializableSession in GAE
      // and don't retrieve session if possible
      final ClassWebResource cwr = getClassWebResource();
      final HttpSession hsess = shallSession(cwr, pi) ? request.getSession(false) : null;
      Object oldsess = null;
      if (hsess == null) {
        oldsess = SessionsCtrl.getRawCurrent();
        SessionsCtrl.setCurrent(new SessionResolverImpl(ctx, request));
        // it might be created later
      }

      WebApp wapp;
      Session sess;
      final Object old =
          hsess != null
              ? (wapp = WebManager.getWebAppIfAny(ctx)) != null
                      && (sess = SessionsCtrl.getSession(wapp, hsess)) != null
                  ? I18Ns.setup(sess, request, response, "UTF-8")
                  : I18Ns.setup(hsess, request, response, "UTF-8")
              : Charsets.setup(null, request, response, "UTF-8");
      try {
        cwr.service(request, response, pi.substring(ClassWebResource.PATH_PREFIX.length()));
      } finally {
        if (hsess != null) I18Ns.cleanup(request, old);
        else {
          Charsets.cleanup(request, old);
          SessionsCtrl.setRawCurrent(oldsess);
        }
      }
      return; // done
    }

    final Session sess = WebManager.getSession(ctx, request, false);
    if (withpi) {
      final AuExtension aue = getAuExtensionByPath(pi);
      if (aue == null) {
        response.sendError(response.SC_NOT_FOUND);
        log.debug("Unknown path info: " + pi);
        return;
      }

      Object oldsess = null;
      if (sess == null) {
        oldsess = SessionsCtrl.getRawCurrent();
        SessionsCtrl.setCurrent(new SessionResolverImpl(ctx, request));
        // it might be created later
      }

      final Object old =
          sess != null
              ? I18Ns.setup(sess, request, response, "UTF-8")
              : Charsets.setup(null, request, response, "UTF-8");
      try {
        aue.service(request, response, pi);
      } finally {
        if (sess != null) I18Ns.cleanup(request, old);
        else {
          Charsets.cleanup(request, old);
          SessionsCtrl.setRawCurrent(oldsess);
        }
      }
      return; // done
    }

    // AU
    if (sess == null) {
      response.setIntHeader("ZK-Error", response.SC_GONE); // denote timeout

      // Bug 1849088: rmDesktop might be sent after invalidate
      // Bug 1859776: need send response to client for redirect or others
      final WebApp wapp = WebManager.getWebAppIfAny(ctx);
      final String dtid = getAuDecoder(wapp).getDesktopId(request);
      if (dtid != null) sessionTimeout(request, response, wapp, dtid);
      return;
    }

    // Feature 3285074 add no-cache for security risk.
    response.setHeader("Pragma", "no-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Cache-Control", "no-store");
    response.setHeader("Expires", "-1");

    final Object old = I18Ns.setup(sess, request, response, "UTF-8");
    try {
      process(sess, request, response);
    } finally {
      I18Ns.cleanup(request, old);
    }
  }