Exemplo n.º 1
0
  /**
   * This method is called when receiving a GO_AWAY from the other peer. We check the close state to
   * act appropriately:
   *
   * <p>* NOT_CLOSED: we move to REMOTELY_CLOSED and queue a disconnect, so that the content of the
   * queue is written, and then the connection closed. We notify the application after being
   * terminated. See <code>HTTP2Session.ControlEntry#succeeded()</code> * In all other cases, we do
   * nothing since other methods are already performing their actions.
   *
   * @param frame the GO_AWAY frame that has been received.
   * @see #close(int, String, Callback)
   * @see #onShutdown()
   * @see #onIdleTimeout()
   */
  @Override
  public void onGoAway(final GoAwayFrame frame) {
    if (LOG.isDebugEnabled()) LOG.debug("Received {}", frame);

    while (true) {
      CloseState current = closed.get();
      switch (current) {
        case NOT_CLOSED:
          {
            if (closed.compareAndSet(current, CloseState.REMOTELY_CLOSED)) {
              // We received a GO_AWAY, so try to write
              // what's in the queue and then disconnect.
              notifyClose(this, frame);
              control(null, Callback.NOOP, new DisconnectFrame());
              return;
            }
            break;
          }
        default:
          {
            if (LOG.isDebugEnabled()) LOG.debug("Ignored {}, already closed", frame);
            return;
          }
      }
    }
  }
Exemplo n.º 2
0
    @Override
    protected SelectChannelEndPoint newEndPoint(
        SocketChannel channel, SelectSet selectSet, SelectionKey key) throws IOException {
      // We're connected, cancel the connect timeout
      Timeout.Task connectTimeout = _connectingChannels.remove(channel);
      if (connectTimeout != null) connectTimeout.cancel();
      if (LOG.isDebugEnabled())
        LOG.debug("Channels with connection pending: {}", _connectingChannels.size());

      // key should have destination at this point (will be replaced by endpoint after this call)
      HttpDestination dest = (HttpDestination) key.attachment();

      SelectChannelEndPoint scep =
          new SelectChannelEndPoint(channel, selectSet, key, (int) _httpClient.getIdleTimeout());
      AsyncEndPoint ep = scep;

      if (dest.isSecure()) {
        LOG.debug("secure to {}, proxied={}", channel, dest.isProxied());
        ep = new UpgradableEndPoint(ep, newSslEngine(channel));
      }

      AsyncConnection connection =
          selectSet.getManager().newConnection(channel, ep, key.attachment());
      ep.setConnection(connection);

      AbstractHttpConnection httpConnection = (AbstractHttpConnection) connection;
      httpConnection.setDestination(dest);

      if (dest.isSecure() && !dest.isProxied()) ((UpgradableEndPoint) ep).upgrade();

      dest.onNewConnection(httpConnection);

      return scep;
    }
Exemplo n.º 3
0
  /**
   * Called to indicate that the last write has been performed. It updates the state and performs
   * cleanup operations.
   */
  void closed() {
    while (true) {
      OutputState state = _state.get();
      switch (state) {
        case CLOSED:
          {
            return;
          }
        case UNREADY:
          {
            if (_state.compareAndSet(state, OutputState.ERROR))
              _writeListener.onError(
                  _onError == null ? new EofException("Async closed") : _onError);
            break;
          }
        default:
          {
            if (!_state.compareAndSet(state, OutputState.CLOSED)) break;

            try {
              _channel.getResponse().closeOutput();
            } catch (Throwable x) {
              if (LOG.isDebugEnabled()) LOG.debug(x);
              abort(x);
            } finally {
              releaseBuffer();
            }
            // Return even if an exception is thrown by closeOutput().
            return;
          }
      }
    }
  }
Exemplo n.º 4
0
  /** @see Proxy#startProxy() */
  public void startProxy() {
    _started = true;

    if (_tx.isCompleted()) throw new IllegalStateException("Transaction has completed");

    if (!_parallel && _actives > 0) return;

    // Patch TMP fix for CIPANGO 8
    CallSession callSession = _tx.getRequest().getCallSession();
    SessionManager cm = callSession.getServer().getSessionManager();

    SessionScope work = cm.openScope(callSession);
    try {
      // End patch
      while (LazyList.size(_targets) > 0) {
        Branch branch = (Branch) LazyList.get(_targets, 0);
        _targets = LazyList.remove(_targets, 0);

        if (LOG.isDebugEnabled()) LOG.debug("Proxying to {} ", branch.getUri(), null);

        branch.start();

        if (!_parallel) break;
      }
      // Patch TMP fix for CIPANGO 8
    } finally {
      work.close();
    }
    // End patch
  }
Exemplo n.º 5
0
  public Object call(Object obj, Object... args) {
    if ((this.pojo == null) || (this.method == null)) {
      LOG.warn("Cannot execute call: pojo={}, method={}", pojo, method);
      return null; // no call event method determined
    }

    if (obj == null) {
      String err = String.format("Cannot call %s on null object", this.method);
      LOG.warn(new RuntimeException(err));
      return null;
    }

    if (args.length < paramTypes.length) {
      throw new IllegalArgumentException(
          "Call arguments length ["
              + args.length
              + "] must always be greater than or equal to captured args length ["
              + paramTypes.length
              + "]");
    }

    try {
      return this.method.invoke(obj, args);
    } catch (Throwable t) {
      String err = formatMethodCallError(args);
      throw unwrapRuntimeException(err, t);
    }
  }
 /**
  * Load user's info from database.
  *
  * @param userName
  */
 @Override
 protected UserIdentity loadUser(String userName) {
   try {
     try (Connection connection = getConnection();
         PreparedStatement statement1 = connection.prepareStatement(_userSql)) {
       statement1.setObject(1, userName);
       try (ResultSet rs1 = statement1.executeQuery()) {
         if (rs1.next()) {
           int key = rs1.getInt(_userTableKey);
           String credentials = rs1.getString(_userTablePasswordField);
           List<String> roles = new ArrayList<String>();
           try (PreparedStatement statement2 = connection.prepareStatement(_roleSql)) {
             statement2.setInt(1, key);
             try (ResultSet rs2 = statement2.executeQuery()) {
               while (rs2.next()) {
                 roles.add(rs2.getString(_roleTableRoleField));
               }
             }
           }
           return putUser(
               userName,
               Credential.getCredential(credentials),
               roles.toArray(new String[roles.size()]));
         }
       }
     }
   } catch (NamingException e) {
     LOG.warn("No datasource for " + _jndiName, e);
   } catch (SQLException e) {
     LOG.warn("Problem loading user info for " + userName, e);
   }
   return null;
 }
Exemplo n.º 7
0
  public void makeTempDirectory(File parent, WebAppContext context, boolean deleteExisting)
      throws IOException {
    if (parent != null && parent.exists() && parent.canWrite() && parent.isDirectory()) {
      String temp = getCanonicalNameForWebAppTmpDir(context);
      File tmpDir = new File(parent, temp);

      if (deleteExisting && tmpDir.exists()) {
        if (!IO.delete(tmpDir)) {
          if (LOG.isDebugEnabled()) LOG.debug("Failed to delete temp dir " + tmpDir);
        }

        // If we can't delete the existing tmp dir, create a new one
        if (tmpDir.exists()) {
          String old = tmpDir.toString();
          tmpDir = File.createTempFile(temp + "_", "");
          if (tmpDir.exists()) IO.delete(tmpDir);
          LOG.warn("Can't reuse " + old + ", using " + tmpDir);
        }
      }

      if (!tmpDir.exists()) tmpDir.mkdir();

      // If the parent is not a work directory
      if (!isTempWorkDirectory(tmpDir)) {
        tmpDir.deleteOnExit();
      }

      if (LOG.isDebugEnabled()) LOG.debug("Set temp dir " + tmpDir);
      context.setTempDirectory(tmpDir);
    }
  }
Exemplo n.º 8
0
  /* ------------------------------------------------------------ */
  public Object invoke(String name, Object[] params, String[] signature)
      throws MBeanException, ReflectionException {
    if (LOG.isDebugEnabled()) LOG.debug("invoke " + name);

    String methodKey = name + "(";
    if (signature != null)
      for (int i = 0; i < signature.length; i++) methodKey += (i > 0 ? "," : "") + signature[i];
    methodKey += ")";

    ClassLoader old_loader = Thread.currentThread().getContextClassLoader();
    try {
      Thread.currentThread().setContextClassLoader(_loader);
      Method method = (Method) _methods.get(methodKey);
      if (method == null) throw new NoSuchMethodException(methodKey);

      Object o = _managed;
      if (method.getDeclaringClass().isInstance(this)) o = this;
      return method.invoke(o, params);
    } catch (NoSuchMethodException e) {
      LOG.warn(Log.EXCEPTION, e);
      throw new ReflectionException(e);
    } catch (IllegalAccessException e) {
      LOG.warn(Log.EXCEPTION, e);
      throw new MBeanException(e);
    } catch (InvocationTargetException e) {
      LOG.warn(Log.EXCEPTION, e);
      throw new ReflectionException(new Exception(e.getCause()));
    } finally {
      Thread.currentThread().setContextClassLoader(old_loader);
    }
  }
Exemplo n.º 9
0
 public static Namelist fromJSONtoNamelist(JSONObject jsonObj) {
   logger.info("Convert json object to namelist...");
   if (jsonObj != null) {
     String name = jsonObj.getString("_name");
     String species = jsonObj.getString("_species");
     if (species == null) {
       JSONObject metadataJSONObj = jsonObj.getJSONObject("metadata");
       if (metadataJSONObj != null) {
         species = metadataJSONObj.getString("species");
       }
     }
     int size = jsonObj.getInt("_size");
     JSONArray data =
         jsonObj.containsKey("_data")
             ? jsonObj.getJSONArray("_data")
             : jsonObj.getJSONArray("gaggle-data");
     String[] names = new String[data.size()];
     for (int i = 0; i < data.size(); i++) {
       logger.info("Data item: " + data.getString(i));
       names[i] = data.getString(i);
     }
     logger.info("Species: " + species + " Names: " + names);
     Namelist nl = new Namelist(name, species, names);
     return nl;
   }
   return null;
 }
Exemplo n.º 10
0
  /**
   * Parse a resource that is a jar file.
   *
   * @param jarResource
   * @param resolver
   * @throws Exception
   */
  public void parseJar(Resource jarResource, final ClassNameResolver resolver) throws Exception {
    if (jarResource == null) return;

    URI uri = jarResource.getURI();
    if (jarResource.toString().endsWith(".jar")) {
      if (LOG.isDebugEnabled()) {
        LOG.debug("Scanning jar {}", jarResource);
      }
      ;

      // treat it as a jar that we need to open and scan all entries from
      InputStream in = jarResource.getInputStream();
      if (in == null) return;

      JarInputStream jar_in = new JarInputStream(in);
      try {
        JarEntry entry = jar_in.getNextJarEntry();
        while (entry != null) {
          parseJarEntry(uri, entry, resolver);
          entry = jar_in.getNextJarEntry();
        }
      } finally {
        jar_in.close();
      }
    }
  }
Exemplo n.º 11
0
  /**
   * Parse a single entry in a jar file
   *
   * @param jar
   * @param entry
   * @param resolver
   * @throws Exception
   */
  protected void parseJarEntry(URI jar, JarEntry entry, final ClassNameResolver resolver)
      throws Exception {
    if (jar == null || entry == null) return;

    // skip directories
    if (entry.isDirectory()) return;

    String name = entry.getName();

    // check file is a valid class file name
    if (isValidClassFileName(name) && isValidClassFilePath(name)) {
      String shortName = name.replace('/', '.').substring(0, name.length() - 6);

      if ((resolver == null)
          || (!resolver.isExcluded(shortName)
              && (!isParsed(shortName) || resolver.shouldOverride(shortName)))) {
        Resource clazz = Resource.newResource("jar:" + jar + "!/" + name);
        if (LOG.isDebugEnabled()) {
          LOG.debug("Scanning class from jar {}", clazz);
        }
        ;
        scanClass(clazz.getInputStream());
      }
    }
  }
Exemplo n.º 12
0
  /**
   * A typical close by a remote peer involves a GO_AWAY frame followed by TCP FIN. This method is
   * invoked when the TCP FIN is received, or when an exception is thrown while reading, and we
   * check the close state to act appropriately:
   *
   * <p>* NOT_CLOSED: means that the remote peer did not send a GO_AWAY (abrupt close) or there was
   * an exception while reading, and therefore we terminate.
   *
   * <p>* LOCALLY_CLOSED: we have sent the GO_AWAY to the remote peer, which received it and closed
   * the connection; we queue a disconnect to close the connection on the local side. The GO_AWAY
   * just shutdown the output, so we need this step to make sure the connection is closed. See
   * {@link #close(int, String, Callback)}.
   *
   * <p>* REMOTELY_CLOSED: we received the GO_AWAY, and the TCP FIN afterwards, so we do nothing
   * since the handling of the GO_AWAY will take care of closing the connection. See {@link
   * #onGoAway(GoAwayFrame)}.
   *
   * @see #onGoAway(GoAwayFrame)
   * @see #close(int, String, Callback)
   * @see #onIdleTimeout()
   */
  @Override
  public void onShutdown() {
    if (LOG.isDebugEnabled()) LOG.debug("Shutting down {}", this);

    switch (closed.get()) {
      case NOT_CLOSED:
        {
          // The other peer did not send a GO_AWAY, no need to be gentle.
          if (LOG.isDebugEnabled()) LOG.debug("Abrupt close for {}", this);
          abort(new ClosedChannelException());
          break;
        }
      case LOCALLY_CLOSED:
        {
          // We have closed locally, and only shutdown
          // the output; now queue a disconnect.
          control(null, Callback.NOOP, new DisconnectFrame());
          break;
        }
      case REMOTELY_CLOSED:
        {
          // Nothing to do, the GO_AWAY frame we
          // received will close the connection.
          break;
        }
      default:
        {
          break;
        }
    }
  }
Exemplo n.º 13
0
  protected IStream createRemoteStream(int streamId) {
    // SPEC: exceeding max concurrent streams is treated as stream error.
    while (true) {
      int remoteCount = remoteStreamCount.get();
      int maxCount = getMaxRemoteStreams();
      if (maxCount >= 0 && remoteCount >= maxCount) {
        reset(new ResetFrame(streamId, ErrorCode.REFUSED_STREAM_ERROR.code), Callback.NOOP);
        return null;
      }
      if (remoteStreamCount.compareAndSet(remoteCount, remoteCount + 1)) break;
    }

    IStream stream = newStream(streamId, false);

    // SPEC: duplicate stream is treated as connection error.
    if (streams.putIfAbsent(streamId, stream) == null) {
      updateLastStreamId(streamId);
      stream.setIdleTimeout(getStreamIdleTimeout());
      flowControl.onStreamCreated(stream);
      if (LOG.isDebugEnabled()) LOG.debug("Created remote {}", stream);
      return stream;
    } else {
      close(ErrorCode.PROTOCOL_ERROR.code, "duplicate_stream", Callback.NOOP);
      return null;
    }
  }
Exemplo n.º 14
0
 /**
  * Invoked internally and by applications to send a GO_AWAY frame to the other peer. We check the
  * close state to act appropriately:
  *
  * <p>* NOT_CLOSED: we move to LOCALLY_CLOSED and queue a GO_AWAY. When the GO_AWAY has been
  * written, it will only cause the output to be shut down (not the connection closed), so that the
  * application can still read frames arriving from the other peer. Ideally the other peer will
  * notice the GO_AWAY and close the connection. When that happen, we close the connection from
  * {@link #onShutdown()}. Otherwise, the idle timeout mechanism will close the connection, see
  * {@link #onIdleTimeout()}.
  *
  * <p>* In all other cases, we do nothing since other methods are already performing their
  * actions.
  *
  * @param error the error code
  * @param reason the reason
  * @param callback the callback to invoke when the operation is complete
  * @see #onGoAway(GoAwayFrame)
  * @see #onShutdown()
  * @see #onIdleTimeout()
  */
 @Override
 public boolean close(int error, String reason, Callback callback) {
   while (true) {
     CloseState current = closed.get();
     switch (current) {
       case NOT_CLOSED:
         {
           if (closed.compareAndSet(current, CloseState.LOCALLY_CLOSED)) {
             byte[] payload = null;
             if (reason != null) {
               // Trim the reason to avoid attack vectors.
               reason = reason.substring(0, Math.min(reason.length(), 32));
               payload = reason.getBytes(StandardCharsets.UTF_8);
             }
             GoAwayFrame frame = new GoAwayFrame(lastStreamId.get(), error, payload);
             control(null, callback, frame);
             return true;
           }
           break;
         }
       default:
         {
           if (LOG.isDebugEnabled())
             LOG.debug("Ignoring close {}/{}, already closed", error, reason);
           callback.succeeded();
           return false;
         }
     }
   }
 }
Exemplo n.º 15
0
  /* Handle a request from a connection.
   * Called to handle a request on the connection when either the header has been received,
   * or after the entire request has been received (for short requests of known length), or
   * on the dispatch of an async request.
   */
  public void handleAsync(AbstractHttpConnection connection) throws IOException, ServletException {
    final AsyncContinuation async = connection.getRequest().getAsyncContinuation();
    final AsyncContinuation.AsyncEventState state = async.getAsyncEventState();

    final Request baseRequest = connection.getRequest();
    final String path = state.getPath();

    if (path != null) {
      // this is a dispatch with a path
      final String contextPath = state.getServletContext().getContextPath();
      HttpURI uri = new HttpURI(URIUtil.addPaths(contextPath, path));
      baseRequest.setUri(uri);
      baseRequest.setRequestURI(null);
      baseRequest.setPathInfo(baseRequest.getRequestURI());
      if (uri.getQuery() != null) baseRequest.mergeQueryString(uri.getQuery());
    }

    final String target = baseRequest.getPathInfo();
    final HttpServletRequest request = (HttpServletRequest) async.getRequest();
    final HttpServletResponse response = (HttpServletResponse) async.getResponse();

    if (LOG.isDebugEnabled()) {
      LOG.debug("REQUEST " + target + " on " + connection);
      handle(target, baseRequest, request, response);
      LOG.debug("RESPONSE " + target + "  " + connection.getResponse().getStatus());
    } else handle(target, baseRequest, request, response);
  }
  private void serviceDeliver(
      String targetId, HttpServletRequest httpRequest, HttpServletResponse httpResponse)
      throws ServletException, IOException {
    if (gateway.getClientDelegate(targetId) == null) {
      // Expired client tries to deliver without handshake
      httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
      return;
    }

    byte[] body = Utils.read(httpRequest.getInputStream());

    RHTTPResponse response = RHTTPResponse.fromFrameBytes(body);

    ExternalRequest externalRequest = gateway.removeExternalRequest(response.getId());
    if (externalRequest != null) {
      externalRequest.respond(response);
      logger.debug(
          "Deliver request from device {}, gateway request {}, response {}",
          new Object[] {targetId, externalRequest, response});
    } else {
      // We can arrive here for a race with the continuation expiration, which expired just before
      // the gateway client responded with a valid response; log this case ignore it.
      logger.debug(
          "Deliver request from device {}, missing gateway request, response {}",
          targetId,
          response);
    }
  }
Exemplo n.º 17
0
  public String expand(String str, Stack<String> seenStack) {
    if (str == null) {
      return str;
    }

    if (str.indexOf("${") < 0) {
      // Contains no potential expressions.
      return str;
    }

    Matcher mat = __propertyPattern.matcher(str);
    StringBuilder expanded = new StringBuilder();
    int offset = 0;
    String property;
    String value;

    while (mat.find(offset)) {
      property = mat.group(1);

      // Loop detection
      if (seenStack.contains(property)) {
        StringBuilder err = new StringBuilder();
        err.append("Property expansion loop detected: ");
        int idx = seenStack.lastIndexOf(property);
        for (int i = idx; i < seenStack.size(); i++) {
          err.append(seenStack.get(i));
          err.append(" -> ");
        }
        err.append(property);
        throw new RuntimeException(err.toString());
      }

      seenStack.push(property);

      // find property name
      expanded.append(str.subSequence(offset, mat.start()));
      // get property value
      value = getString(property);
      if (value == null) {
        if (LOG.isDebugEnabled()) LOG.debug("Unable to expand: {}", property);
        expanded.append(mat.group());
      } else {
        // recursively expand
        value = expand(value, seenStack);
        expanded.append(value);
      }
      // update offset
      offset = mat.end();
    }

    // leftover
    expanded.append(str.substring(offset));

    // special case for "$$"
    if (expanded.indexOf("$$") >= 0) {
      return expanded.toString().replaceAll("\\$\\$", "\\$");
    }

    return expanded.toString();
  }
Exemplo n.º 18
0
 @Override
 public void shutdown() {
   // loop over all the sessions in memory (a few times if necessary to catch sessions that have
   // been
   // added while we're running
   int loop = 100;
   while (!_sessions.isEmpty() && loop-- > 0) {
     for (Session session : _sessions.values()) {
       // if we have a backing store and the session is dirty make sure it is written out
       if (_sessionDataStore != null) {
         if (session.getSessionData().isDirty()) {
           session.willPassivate();
           try {
             _sessionDataStore.store(session.getId(), session.getSessionData());
           } catch (Exception e) {
             LOG.warn(e);
           }
         }
         doDelete(session.getId()); // remove from memory
       } else {
         // not preserving sessions on exit
         try {
           session.invalidate();
         } catch (Exception e) {
           LOG.ignore(e);
         }
       }
     }
   }
 }
Exemplo n.º 19
0
  /**
   * Add a name to object binding to this Context.
   *
   * @param name a <code>Name</code> value
   * @param obj an <code>Object</code> value
   */
  public void addBinding(Name name, Object obj) throws NameAlreadyBoundException {
    String key = name.toString();
    Binding binding = new Binding(key, obj);

    Collection<Listener> list = findListeners();

    for (Listener listener : list) {
      binding = listener.bind(this, binding);
      if (binding == null) break;
    }

    if (__log.isDebugEnabled())
      __log.debug(
          "Adding binding with key="
              + key
              + " obj="
              + obj
              + " for context="
              + _name
              + " as "
              + binding);

    if (binding != null) {
      if (_bindings.containsKey(key)) {
        if (_supportDeepBinding) {
          // quietly return (no exception)
          // this is jndi spec breaking, but is added to support broken
          // jndi users like openejb.
          return;
        }
        throw new NameAlreadyBoundException(name.toString());
      }
      _bindings.put(key, binding);
    }
  }
Exemplo n.º 20
0
 @Override
 public void onClose() {
   super.onClose();
   if (LOG.isDebugEnabled()) LOG.debug("onClose {}", this);
   _writeFlusher.onClose();
   _fillInterest.onClose();
 }
  public GatewayServer(
      String contextPath,
      String externalServletPath,
      String gatewayServletPath,
      TargetIdRetriever targetIdRetriever) {
    HandlerCollection handlers = new HandlerCollection();
    setHandler(handlers);
    context = new ServletContextHandler(handlers, contextPath, ServletContextHandler.SESSIONS);

    // Setup the gateway
    gateway = createGateway();

    // Setup external servlet
    ExternalServlet externalServlet = new ExternalServlet(gateway, targetIdRetriever);
    externalServletHolder = new ServletHolder(externalServlet);
    context.addServlet(externalServletHolder, externalServletPath + "/*");
    logger.debug("External servlet mapped to {}/*", externalServletPath);

    // Setup gateway servlet
    ConnectorServlet gatewayServlet = new ConnectorServlet(gateway);
    connectorServletHolder = new ServletHolder(gatewayServlet);
    connectorServletHolder.setInitParameter("clientTimeout", "15000");
    context.addServlet(connectorServletHolder, gatewayServletPath + "/*");
    logger.debug("Gateway servlet mapped to {}/*", gatewayServletPath);
  }
Exemplo n.º 22
0
 /**
  * Sends an error 500, performing a special logic to detect whether the request is suspended, to
  * avoid concurrent writes from the application.
  *
  * <p>It may happen that the application suspends, and then throws an exception, while an
  * application spawned thread writes the response content; in such case, we attempt to commit the
  * error directly bypassing the {@link ErrorHandler} mechanisms and the response OutputStream.
  *
  * @param x the Throwable that caused the problem
  */
 protected void handleException(Throwable x) {
   try {
     _request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, x);
     _request.setAttribute(RequestDispatcher.ERROR_EXCEPTION_TYPE, x.getClass());
     if (_state.isSuspended()) {
       HttpFields fields = new HttpFields();
       fields.add(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE);
       ResponseInfo info =
           new ResponseInfo(
               _request.getHttpVersion(),
               fields,
               0,
               HttpStatus.INTERNAL_SERVER_ERROR_500,
               null,
               _request.isHead());
       boolean committed = sendResponse(info, null, true);
       if (!committed) LOG.warn("Could not send response error 500: " + x);
       _request.getAsyncContext().complete();
     } else if (isCommitted()) {
       if (!(x instanceof EofException)) LOG.warn("Could not send response error 500: " + x);
     } else {
       _response.setHeader(HttpHeader.CONNECTION.asString(), HttpHeaderValue.CLOSE.asString());
       _response.sendError(500, x.getMessage());
     }
   } catch (IOException e) {
     // We tried our best, just log
     LOG.debug("Could not commit response error 500", e);
   }
 }
Exemplo n.º 23
0
  @Override
  public void onFillable() {
    // onFillable means that there are encrypted bytes ready to be filled.
    // however we do not fill them here on this callback, but instead wakeup
    // the decrypted readInterest and/or writeFlusher so that they will attempt
    // to do the fill and/or flush again and these calls will do the actually
    // filling.

    if (DEBUG) LOG.debug("onFillable enter {}", getEndPoint());

    // We have received a close handshake, close the end point to send FIN.
    if (_decryptedEndPoint.isInputShutdown()) getEndPoint().close();

    // wake up whoever is doing the fill or the flush so they can
    // do all the filling, unwrapping, wrapping and flushing
    _decryptedEndPoint.getFillInterest().fillable();

    // If we are handshaking, then wake up any waiting write as well as it may have been blocked on
    // the read
    synchronized (_decryptedEndPoint) {
      if (_decryptedEndPoint._flushRequiresFillToProgress) {
        _decryptedEndPoint._flushRequiresFillToProgress = false;
        getExecutor().execute(_runCompletWrite);
      }
    }

    if (DEBUG) LOG.debug("onFillable exit {}", getEndPoint());
  }
Exemplo n.º 24
0
  /**
   * Construct a resource from a string.
   *
   * @param resource A URL or filename.
   * @param useCaches controls URLConnection caching
   * @return A Resource object.
   * @throws MalformedURLException Problem accessing URI
   */
  public static Resource newResource(String resource, boolean useCaches)
      throws MalformedURLException {
    URL url = null;
    try {
      // Try to format as a URL?
      url = new URL(resource);
    } catch (MalformedURLException e) {
      if (!resource.startsWith("ftp:")
          && !resource.startsWith("file:")
          && !resource.startsWith("jar:")) {
        try {
          // It's a file.
          if (resource.startsWith("./")) resource = resource.substring(2);

          File file = new File(resource).getCanonicalFile();
          return new FileResource(file);
        } catch (Exception e2) {
          LOG.debug(Log.EXCEPTION, e2);
          throw e;
        }
      } else {
        LOG.warn("Bad Resource: " + resource);
        throw e;
      }
    }

    return newResource(url);
  }
  /**
   * Check to see if the ServletContainerIntializer loaded via the ServiceLoader came from a jar
   * that is excluded by the fragment ordering. See ServletSpec 3.0 p.85.
   *
   * @param context
   * @param sci
   * @return true if excluded
   */
  public boolean isFromExcludedJar(
      WebAppContext context, ServletContainerInitializer sci, Resource sciResource)
      throws Exception {
    if (sci == null) throw new IllegalArgumentException("ServletContainerInitializer null");
    if (context == null) throw new IllegalArgumentException("WebAppContext null");

    if (LOG.isDebugEnabled()) LOG.debug("Checking {} for jar exclusion", sci);

    // A ServletContainerInitializer that came from the container's classpath cannot be excluded by
    // an ordering
    // of WEB-INF/lib jars
    if (isFromContainerClassPath(context, sci)) return false;

    List<Resource> orderedJars = context.getMetaData().getOrderedWebInfJars();

    // If no ordering, nothing is excluded
    if (context.getMetaData().getOrdering() == null) return false;

    // there is an ordering, but there are no jars resulting from the ordering, everything excluded
    if (orderedJars.isEmpty()) return true;

    if (sciResource == null)
      return false; // not from a jar therefore not from WEB-INF so not excludable

    URI loadingJarURI = sciResource.getURI();
    boolean found = false;
    Iterator<Resource> itor = orderedJars.iterator();
    while (!found && itor.hasNext()) {
      Resource r = itor.next();
      found = r.getURI().equals(loadingJarURI);
    }

    return !found;
  }
  /*------------------------------------------------------------ */
  @Override
  public void doStart() throws Exception {
    super.doStart();
    __log.debug("KVStoreSessionManager::doStart method");
    String kvstorename = this._session_id_manager.getKvstorename();
    if (kvstorename == null) throw new IllegalStateException("kvstore name is null");
    String kvstorehosts = this._session_id_manager.getKvstorehosts();
    if (kvstorehosts == null) throw new IllegalStateException("kvstore hosts list is null");
    this._kvstorehandler = this._session_id_manager.getKVStore();
    String[] hhosts = this._session_id_manager.getKvstorehosts().split(",");
    KVStoreConfig kconfig = new KVStoreConfig(this._session_id_manager.getKvstorename(), hhosts);
    KVStore kvstore = KVStoreFactory.getStore(kconfig);
    if (kvstore == null)
      throw new IllegalStateException(
          "cannot connect to kvstore, hosts=" + kvstorehosts + ";storename=" + kvstorename);
    else __log.debug("succesfully connected to the kvstore instance");
    this._kvstorehandler = kvstore;

    if (this._kvstorehandler == null)
      throw new IllegalStateException("kvstore handler passed from session id manager is null");
    String[] hosts = getContextHandler().getVirtualHosts();

    if (hosts == null || hosts.length == 0) hosts = new String[] {"::"}; // IPv6 equiv of 0.0.0.0

    String contextPath = getContext().getContextPath();
    if (contextPath == null || "".equals(contextPath)) {
      contextPath = "*";
    }

    _contextId = createContextId(hosts, contextPath);
    this.kvstore_object_ops = new ArrayList<Operation>();
  }
Exemplo n.º 27
0
  /* ------------------------------------------------------------ */
  @Override
  public synchronized String[] list() {
    if (isDirectory() && _list == null) {
      List<String> list = null;
      try {
        list = listEntries();
      } catch (Exception e) {
        // Sun's JarURLConnection impl for jar: protocol will close a JarFile in its connect()
        // method if
        // useCaches == false (eg someone called URLConnection with defaultUseCaches==true).
        // As their sun.net.www.protocol.jar package caches JarFiles and/or connections, we can wind
        // up in
        // the situation where the JarFile we have remembered in our _jarFile member has actually
        // been closed
        // by other code.
        // So, do one retry to drop a connection and get a fresh JarFile
        LOG.warn("Retrying list:" + e);
        LOG.debug(e);
        release();
        list = listEntries();
      }

      if (list != null) {
        _list = new String[list.size()];
        list.toArray(_list);
      }
    }
    return _list;
  }
Exemplo n.º 28
0
  @Override
  public void bind(InetSocketAddress addr, int backlog) throws IOException {
    // check if there is already a connector listening
    Collection<NetworkConnector> connectors = _server.getBeans(NetworkConnector.class);
    if (connectors != null) {
      for (NetworkConnector connector : connectors) {
        if (connector.getPort() == addr.getPort()) {
          if (LOG.isDebugEnabled())
            LOG.debug("server already bound to port " + addr.getPort() + ", no need to rebind");
          return;
        }
      }
    }

    if (_serverShared) throw new IOException("jetty server is not bound to port " + addr.getPort());

    this._addr = addr;

    if (LOG.isDebugEnabled()) LOG.debug("binding server to port " + addr.getPort());
    ServerConnector connector = new ServerConnector(_server);
    connector.setPort(addr.getPort());
    connector.setHost(addr.getHostName());
    _server.addConnector(connector);

    _connectors.put(addr.getHostName() + addr.getPort(), connector);
  }
Exemplo n.º 29
0
  /**
   * Asynchronous send of HTTP content.
   *
   * @param httpContent The HTTP content to send
   * @param callback The callback to use to notify success or failure
   */
  public void sendContent(HttpContent httpContent, Callback callback) {
    if (LOG.isDebugEnabled()) LOG.debug("sendContent(http={},{})", httpContent, callback);

    if (BufferUtil.hasContent(_aggregate)) {
      callback.failed(new IOException("cannot sendContent() after write()"));
      return;
    }
    if (_channel.isCommitted()) {
      callback.failed(new IOException("committed"));
      return;
    }

    while (true) {
      switch (_state.get()) {
        case OPEN:
          if (!_state.compareAndSet(OutputState.OPEN, OutputState.PENDING)) continue;
          break;

        case ERROR:
          callback.failed(new EofException(_onError));
          return;

        case CLOSED:
          callback.failed(new EofException("Closed"));
          return;

        default:
          throw new IllegalStateException();
      }
      break;
    }

    ByteBuffer buffer = _channel.useDirectBuffers() ? httpContent.getDirectBuffer() : null;
    if (buffer == null) buffer = httpContent.getIndirectBuffer();

    if (buffer != null) {
      sendContent(buffer, callback);
      return;
    }

    try {
      ReadableByteChannel rbc = httpContent.getReadableByteChannel();
      if (rbc != null) {
        // Close of the rbc is done by the async sendContent
        sendContent(rbc, callback);
        return;
      }

      InputStream in = httpContent.getInputStream();
      if (in != null) {
        sendContent(in, callback);
        return;
      }

      throw new IllegalArgumentException("unknown content for " + httpContent);
    } catch (Throwable th) {
      abort(th);
      callback.failed(th);
    }
  }
Exemplo n.º 30
0
 @Override
 public void sendPong(ByteBuffer data) throws IOException, IllegalArgumentException {
   if (LOG.isDebugEnabled()) {
     LOG.debug("sendPong({})", BufferUtil.toDetailString(data));
   }
   jettyRemote.sendPong(data);
 }