Exemplo n.º 1
0
  @Override
  public Cursor query(
      Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

    try {
      HttpUriRequest request = queryRequest(uri, projection, selection, selectionArgs, sortOrder);

      if (DEBUG) Log.i(TAG, "will query: " + request.getURI());

      Cursor cursor = getQueryHandler(uri).handleResponse(httpClient.execute(request));
      // httpClient.getConnectionManager().shutdown();
      return cursor;
    } catch (ConnectException e) {
      Log.w(TAG, "an error occured in query", e);
      return ErrorCursor.getCursor(0, e.getMessage());
    } catch (ClientProtocolException e) {
      Log.w(TAG, "an error occured in query", e);
      return ErrorCursor.getCursor(0, e.getMessage());
    } catch (IOException e) {
      Log.w(TAG, "an error occured in query", e);
      return ErrorCursor.getCursor(0, e.getMessage());
    } catch (IllegalArgumentException e) {
      Log.w(TAG, "an error occured in query", e);
      return ErrorCursor.getCursor(
          0,
          "Unknown URI (not in cache or not answerable by the implementator): " + uri.toString());
    }
  }
Exemplo n.º 2
0
  int invokeOperation(String operation) {
    Authenticator.setDefault(getAuthenticator());

    String urlString = getInvokeUrl() + "&operation=" + operation;
    try {
      URL invoke = new URL(urlString);
      HttpURLConnection connection = (HttpURLConnection) invoke.openConnection();
      connection.setReadTimeout(getHttpRequestReadTimeout());
      InputStream in = connection.getInputStream();

      int ch;
      while ((ch = in.read()) != -1) {
        System.out.write((char) ch);
      }
      in.close();
      System.out.println("");
      System.out.flush();
    } catch (final ConnectException e) {
      LOG.error("error when attempting to fetch URL \"{}\"", urlString, e);
      if (isVerbose()) {
        System.out.println(e.getMessage() + " when attempting to fetch URL \"" + urlString + "\"");
      }
      return 1;
    } catch (final Throwable t) {
      LOG.error("error invoking {} operation", operation, t);
      System.out.println("error invoking " + operation + " operation");
      return 1;
    }

    return 0;
  }
Exemplo n.º 3
0
  public static ArrayList<String> trans(String url)
      throws FailingHttpStatusCodeException, MalformedURLException, IOException {

    ArrayList<String> hrefList = new ArrayList<String>();
    WebClient webClient = new WebClient(BrowserVersion.CHROME);
    webClient.getOptions().setJavaScriptEnabled(false);
    webClient.getOptions().setCssEnabled(false);
    try {
      HtmlPage page = null;
      try {
        page = (HtmlPage) webClient.getPage(url);
      } catch (ConnectException e) {
        System.out.println("Connect fails here:" + e.getMessage());
      }
      InputStream temp = new ByteArrayInputStream(page.asText().getBytes());
      InputStreamReader isr = new InputStreamReader(temp);
      BufferedReader br = new BufferedReader(isr);
      String str = null, rs = null;
      while ((str = br.readLine()) != null) {
        rs = str;
        // System.out.println(rs);
        if (rs != null) hrefList.add(rs);
      }
      System.out.println("从该网址查找的可能相关文本如下:");
      for (int i = 0; i < hrefList.size(); i++) {
        String string = hrefList.get(i);
        string = getTextFromHtml(string);
        if (string.length() >= 30) System.out.println("------" + i + ":" + string);
      }
    } catch (IOException e) {
    }
    return hrefList;
  }
Exemplo n.º 4
0
  /** Program Entry Point. */
  public static void main(String args[]) {

    // Set up log
    Log log = new Log();
    log.setLogName("Client");

    // Connection object listening on 4001
    Connection conn = new ConnectionImpl2(4001);
    InetAddress addr; // will hold address of host to connect to
    try {
      // get address of local host and connect
      addr = InetAddress.getLocalHost();
      conn.connect(addr, 5555);
      // send two messages to server
      conn.send("Client: Hello Server! Are you there?");
      conn.send("Client: Hi again!");
      // write a message in the log and close the connection
      Log.writeToLog("Client is now closing the connection!", "TestApplication");
      conn.close();
    } catch (ConnectException e) {
      Log.writeToLog(e.getMessage(), "TestApplication");
      e.printStackTrace();
    } catch (UnknownHostException e) {
      Log.writeToLog(e.getMessage(), "TestApplication");
      e.printStackTrace();
    } catch (IOException e) {
      Log.writeToLog(e.getMessage(), "TestApplication");
      e.printStackTrace();
    }

    System.out.println("CLIENT TEST FINISHED");
    Log.writeToLog("CLIENT TEST FINISHED", "TestApplication");
  }
Exemplo n.º 5
0
  /**
   * Аутентификация пользователя по паролю.
   *
   * @param answer ссылка на JSONObject ответа для клиента
   * @param user логин
   * @param pass пароль
   * @throws Exception
   */
  private void authenticationByPassword(
      HttpServletRequest request,
      HttpServletResponse response,
      JSONObject answer,
      String user,
      String pass)
      throws Exception {
    Mongo myMongoServ = new Mongo();
    myMongoServ.initMongoConnect();
    if (user.length() > 0) {
      try {
        List<UsersEntity> users =
            (List<UsersEntity>) myMongoServ.find(new UsersEntity(), "username", user);
        if (users.size() > 0) {
          if (users.get(0).getPassword().equals(pass)) {
            String session = UUID.randomUUID().toString();
            // System.out.println("session = "+session);
            myMongoServ.update(users.get(0), "session", session);
            myMongoServ.update(users.get(0), "last_login", new Date().getTime() + "");
            Cookie cuk = new Cookie("JSESSIONID", session);
            cuk.setPath("/phonebk");
            cuk.setHttpOnly(true);
            cuk.setMaxAge(UpdateCookie.COOKIE_MAX_AGE);
            response.addCookie(cuk);

            answer.put("answer", "Auth by pass is correct!");
            answer.put("code", 200);
          } else {
            answer.put("answer", "Password is invalid!");
            answer.put("code", 401);
          }
        } else {
          answer.put("answer", "Username or password is invalid!");
          answer.put("code", 401);
        }
      } catch (MongoSocketOpenException e) {
        System.out.println("Mongo ex");
        System.out.println(e.getMessage());
      } catch (ConnectException e) {
        System.out.println("Connect ex");
        System.out.println(e.getMessage());
      } catch (MongoTimeoutException e) {
        answer.put("answer", "Server is unavailable!");
        answer.put("code", 503);
      }

    } else {
      answer.put("answer", "Username is empty!");
      answer.put("code", 401);
    }
  }
Exemplo n.º 6
0
  @Override
  public Socket establishConnection() {
    try {
      Socket sock = new Socket(server, port);
      return sock;
    } catch (ConnectException cex) {
      getImplementation().fireError(cex.getMessage());
    } catch (Exception ex) {
      ex.printStackTrace();
      getImplementation().fireError(ex.getMessage());
    }

    return null;
  }
Exemplo n.º 7
0
 /** Should only be invoked by the IOLoop */
 @Override
 public void handleConnect(SelectionKey key) throws IOException {
   logger.debug("handle connect...");
   SocketChannel sc = (SocketChannel) channel;
   if (sc.isConnectionPending()) {
     try {
       sc.finishConnect();
       invokeConnectSuccessfulCallback();
       interestOps &= ~SelectionKey.OP_CONNECT;
       IOLoop.INSTANCE.updateHandler(channel, interestOps |= SelectionKey.OP_READ);
     } catch (ConnectException e) {
       logger.warn("Connect failed: {}", e.getMessage());
       invokeConnectFailureCallback(e);
     }
   }
 }
Exemplo n.º 8
0
 public static CommandMinaClient getCommandMinaClient() {
   if (commandMinaClient == null) {
     synchronized (CommandMinaClient.class) {
       if (commandMinaClient == null) {
         try {
           commandMinaClient = new CommandMinaClient();
         } catch (java.net.ConnectException msg) {
           log.error("服务器没有开启,错误信息为:" + msg.getMessage().toString());
         } catch (Exception msg) {
           log.error("客户端连接服务器是发生错误,错误信息为:" + msg.getMessage().toString());
         }
       }
     }
   }
   return commandMinaClient;
 }
Exemplo n.º 9
0
 protected void send(Message message, long timestamp) {
   if (isDisabled()) {
     return;
   }
   try {
     transport.send(message.encoded(), timestamp);
   } catch (ConnectException e) {
     LOG.log(Level.SEVERE, e.getMessage(), e);
     // TODO Add backing off in case of errors
   } catch (FileNotFoundException e) {
     LOG.log(Level.SEVERE, e.getMessage(), e);
     // TODO Add backing off in case of errors
   } catch (IOException e) {
     LOG.log(Level.SEVERE, e.getMessage(), e);
   }
 }
Exemplo n.º 10
0
  /** Program Entry Point. */
  public static void main(String args[]) {

    // Set up log
    Log log = new Log();
    log.setLogName("Client");

    // Connection object listening on 4001
    Connection conn = new ConnectionImpl(4001);
    InetAddress addr; // will hold address of host to connect to
    try {
      // get address of local host and connect
      addr = InetAddress.getLocalHost();
      conn.connect(addr, 5555);
      // send two messages to server
      for (int i = 0; i < 20; i++) {
        conn.send("Packet " + i);
      }
      int rec = 0;
      try {
        while (rec < 20) {
          String msg = conn.receive();
          Log.writeToLog("Message got through to client: " + msg, "TestClient");
          rec++;
        }
      } catch (EOFException e) {
        Log.writeToLog("Got close request (EOFException), closing.", "TestClient");
        conn.close();
      }

      // write a message in the log and close the connection
      Log.writeToLog("Client is now closing the connection!", "TestApplication");
      conn.close();
    } catch (ConnectException e) {
      Log.writeToLog(e.getMessage(), "TestApplication");
      e.printStackTrace();
    } catch (UnknownHostException e) {
      Log.writeToLog(e.getMessage(), "TestApplication");
      e.printStackTrace();
    } catch (IOException e) {
      Log.writeToLog(e.getMessage(), "TestApplication");
      e.printStackTrace();
    }

    System.out.println("CLIENT TEST FINISHED");
    Log.writeToLog("CLIENT TEST FINISHED", "TestApplication");
  }
Exemplo n.º 11
0
  public void connect() throws IOException {
    try {
      connection = new Socket(host, port);
      connection.setSoTimeout(timeout);

      out = new BufferedOutputStream(connection.getOutputStream());
      in = new BufferedInputStream(connection.getInputStream());
    } catch (UnknownHostException e) {
      logger.error("Ha ocurrido un error:", e);
      throw new UnknownHostException(e.getMessage());
    } catch (java.net.ConnectException e) {
      logger.error("Ha ocurrido un error:", e);
      throw new java.net.ConnectException(e.getMessage());
    } catch (IOException e) {
      logger.error("Ha ocurrido un error:", e);
      throw new IOException(e.getMessage());
    }
  }
Exemplo n.º 12
0
  /**
   * 执行http get请求
   *
   * @param url
   * @return HttpResult
   */
  public HttpResult invoke(String url) {
    GetMethod get = null;
    HttpResult result = null;
    try {
      get = new GetMethod(url);
      getClient().executeMethod(get);
      int statusCode = get.getStatusCode();
      String statusText = get.getStatusText();
      result = new HttpResult(statusCode, statusText);

      if (statusCode == 200) {
        StringBuffer buf = new StringBuffer();
        BufferedReader br =
            new BufferedReader(new InputStreamReader(get.getResponseBodyAsStream()));
        String line;
        while ((line = br.readLine()) != null) {
          buf.append(line).append('\n');
        }
        result.setBody(buf.toString());
      }

    } catch (SocketTimeoutException e) {
      result = new HttpResult(408, "REQUEST TIMEOUT. " + e.getMessage());
    } catch (java.net.ConnectException e) {
      result = new HttpResult(321, "Connection refused. " + e.getMessage());
    } catch (Exception e) {
      LOG.error(e);
      result = new HttpResult(123, e.getMessage());
    } finally {
      if (get != null) {
        try {
          // byte[] bytes = get.getResponseBody();
          get.releaseConnection();
        } catch (Exception e) {
          LOG.error(e);
        }
      }
    }

    return result;
  }
Exemplo n.º 13
0
  /**
   * Initialize a server on the given port. This server will not listen until it is launched with
   * the start() method.
   *
   * @param port
   * @throws IOException
   */
  public Server(int port, int botPort, Retriever ret, Map<String, Double> traffic)
      throws IOException {
    if (port <= 1024 || botPort <= 1024) {
      throw new IllegalArgumentException("Ports under 1024 are reserved!");
    }

    _port = port;
    _clients = new ClientPool();
    _socket = new ServerSocket(port);
    try {
      _botSocket = new Socket("localhost", botPort);
      _botConnectionSuccess = true;
      _trafficInput = new BufferedReader(new InputStreamReader(_botSocket.getInputStream()));
      _trafficData = traffic;
    } catch (ConnectException e) {
      System.out.println("unable to connect");
      System.out.println(e.getMessage());
      _botConnectionSuccess = false;
    }
    r = ret;
  }
  public void run() {
    try {
      GuiConnecting.setNetClientHandler(
          this.connectingGui, new NetClientHandler(this.mc, this.ip, this.port));

      if (GuiConnecting.isCancelled(this.connectingGui)) {
        return;
      }

      GuiConnecting.getNetClientHandler(this.connectingGui)
          .addToSendQueue(new Packet2Handshake(this.mc.session.username, this.ip, this.port));
    } catch (UnknownHostException var2) {
      if (GuiConnecting.isCancelled(this.connectingGui)) {
        return;
      }

      this.mc.displayGuiScreen(
          new GuiDisconnected(
              "connect.failed",
              "disconnect.genericReason",
              new Object[] {"Unknown host \'" + this.ip + "\'"}));
    } catch (ConnectException var3) {
      if (GuiConnecting.isCancelled(this.connectingGui)) {
        return;
      }

      this.mc.displayGuiScreen(
          new GuiDisconnected(
              "connect.failed", "disconnect.genericReason", new Object[] {var3.getMessage()}));
    } catch (Exception var4) {
      if (GuiConnecting.isCancelled(this.connectingGui)) {
        return;
      }

      var4.printStackTrace();
      this.mc.displayGuiScreen(
          new GuiDisconnected(
              "connect.failed", "disconnect.genericReason", new Object[] {var4.toString()}));
    }
  }
Exemplo n.º 15
0
  protected boolean execStreamConnect(Properties props) {
    try {
      if (props == null) {
        notifyStreamResult(true, "I2P_ERROR", "No parameters specified in STREAM CONNECT message");
        _log.debug("No parameters specified in STREAM CONNECT message");
        return false;
      }
      boolean verbose = props.getProperty("SILENT", "false").equals("false");

      String dest = props.getProperty("DESTINATION");
      if (dest == null) {
        notifyStreamResult(verbose, "I2P_ERROR", "Destination not specified in RAW SEND message");
        _log.debug("Destination not specified in RAW SEND message");
        return false;
      }
      props.remove("DESTINATION");

      try {
        streamSession.connect(this, dest, props);
        return true;
      } catch (DataFormatException e) {
        _log.debug("Invalid destination in STREAM CONNECT message");
        notifyStreamResult(verbose, "INVALID_KEY", null);
      } catch (ConnectException e) {
        _log.debug("STREAM CONNECT failed: " + e.getMessage());
        notifyStreamResult(verbose, "CONNECTION_REFUSED", null);
      } catch (NoRouteToHostException e) {
        _log.debug("STREAM CONNECT failed: " + e.getMessage());
        notifyStreamResult(verbose, "CANT_REACH_PEER", null);
      } catch (InterruptedIOException e) {
        _log.debug("STREAM CONNECT failed: " + e.getMessage());
        notifyStreamResult(verbose, "TIMEOUT", null);
      } catch (I2PException e) {
        _log.debug("STREAM CONNECT failed: " + e.getMessage());
        notifyStreamResult(verbose, "I2P_ERROR", e.getMessage());
      }
    } catch (IOException e) {
    }
    return false;
  }
  /**
   * Connects this socket to the specified remote host address/port.
   *
   * @param anAddr the remote host address to connect to
   * @param aPort the remote port to connect to
   * @param timeout a timeout where supported. 0 means no timeout
   * @throws IOException if an error occurs while connecting
   */
  private void connect(InetAddress anAddr, int aPort, int timeout) throws IOException {

    InetAddress normalAddr = anAddr.isAnyLocalAddress() ? InetAddress.getLocalHost() : anAddr;
    try {
      if (streaming) {
        if (NetUtil.usingSocks(proxy)) {
          socksConnect(anAddr, aPort, 0);
        } else {
          if (timeout == 0) {
            netImpl.connect(fd, trafficClass, normalAddr, aPort);
          } else {
            netImpl.connectStreamWithTimeoutSocket(fd, aPort, timeout, trafficClass, normalAddr);
          }
        }
      } else {
        netImpl.connectDatagram(fd, aPort, trafficClass, normalAddr);
      }
    } catch (ConnectException e) {
      throw new ConnectException(anAddr + ":" + aPort + " - " + e.getMessage());
    }
    super.address = normalAddr;
    super.port = aPort;
  }
Exemplo n.º 17
0
  private boolean connect(final int retries, final int retryInterval) throws InterruptedException {

    if (logger.isLoggable(Level.FINER)) {
      logger.finer(log.entry("connect", retries, retryInterval));
    }

    if (this.isConnected) return true;

    for (int i = 0; i < retries; i++) {
      logger.fine(log.msg("attempting to establish connection on port " + this.servicePort));

      try {
        this.socket = new Socket();
        InetSocketAddress address =
            new InetSocketAddress(InetAddress.getLocalHost(), this.servicePort);
        logger.fine(
            log.msg(
                "attempt "
                    + (i + 1)
                    + " to connect running service at "
                    + address.getAddress().getHostAddress()
                    + ":"
                    + address.getPort()));
        this.socket.connect(address);
        logger.fine(log.msg("connected to running service instance"));
        this.isConnected = true;
        if (this.listener != null) {
          this.listener.onConnect();
          Thread thread =
              new Thread() {
                @Override
                public void run() {
                  try {
                    ServiceLauncher.this.socket.getInputStream().read();
                  } catch (IOException e) {
                  }
                  if (ServiceLauncher.this.isConnected) {
                    ServiceLauncher.this.isConnected = false;
                    ServiceLauncher.this.listener.onDisconnect();
                  }
                }
              };
          thread.setDaemon(true);
          thread.start();
        }
        return true;
      } catch (ConnectException e) {
        logger.fine(log.msg("cannot connect to port " + this.servicePort + " - " + e.getMessage()));
        Thread.sleep(retryInterval);
      } catch (UnknownHostException e) {
        logger.warning(
            log.msg("cannot connect on port " + this.servicePort + " - " + e.getMessage()));
        break;
      } catch (IOException e) {
        logger.warning(
            log.msg("cannot connect on port " + this.servicePort + " - " + e.getMessage()));
        e.printStackTrace();
        break;
      }
    }
    return false;
  }
Exemplo n.º 18
0
  private void removeLegalHold(
      NewObjectIdentifier oid, String legalHold, ConnectionFactory.DiskConnection[] connections) {

    if (LOG.isLoggable(Level.FINE)) {
      LOG.fine("removeLegalHold has been called for oid " + oid);
    }

    String sysCacheId = CacheClientInterface.SYSTEM_CACHE;

    // Precompute the connection
    Socket socket = null;

    for (int i = 0; i < connections.length; i++) {
      try {
        socket = ConnectionFactory.connect(connections[i]);
        ObjectBroker broker = new ObjectBroker(socket);
        broker.launchRemoveLegalHold(sysCacheId, oid, legalHold, connections[i].getDisks());
      } catch (ConnectException e) {
        LOG.warning(
            "Couldn't remove the "
                + sysCacheId
                + " legal hold for ["
                + oid
                + "] from ["
                + connections[i].getNodeAddress()
                + "]. Error is ["
                + e.getMessage()
                + "]");

        String str = BundleAccess.getInstance().getBundle().getString("err.emd.removeLegalHold.io");

        Object[] args = {connections[i].toString()};
        LOG.log(ExtLevel.EXT_SEVERE, MessageFormat.format(str, args));

      } catch (IOException e) {
        LOG.log(
            Level.SEVERE,
            "Couldn't remove the "
                + sysCacheId
                + " legal hold for ["
                + oid
                + "] from ["
                + connections[i].getNodeAddress()
                + "]",
            e);

        String str = BundleAccess.getInstance().getBundle().getString("err.emd.removeMetadata.io");
        Object[] args = {connections[i].toString()};
        LOG.log(ExtLevel.EXT_SEVERE, MessageFormat.format(str, args));

      } catch (EMDException e) {
        LOG.log(
            Level.SEVERE,
            "Couldn't remove the "
                + sysCacheId
                + " legal hold for ["
                + oid
                + "] from ["
                + connections[i].getNodeAddress()
                + "]",
            e);
        String str =
            BundleAccess.getInstance().getBundle().getString("err.emd.removeMetadata.cache");
        Object[] args = {connections[i].toString()};
        LOG.log(ExtLevel.EXT_SEVERE, MessageFormat.format(str, args));

      } finally {
        if (socket != null) {
          try {
            socket.close();
          } catch (IOException ignored) {
          }
          socket = null;
        }
      }
    }
  }
Exemplo n.º 19
0
  /** Private addLegalHold * */
  private void addLegalHold(
      ConnectionFactory.DiskConnection[] connections, NewObjectIdentifier oid, String legalHold) {

    if (LOG.isLoggable(Level.FINE)) {
      LOG.fine("Ading (" + oid + ", " + legalHold + ")");
    }

    if (connections == null) {
      LOG.severe("connections is null");
      return;
    }

    String sysCacheId = CacheClientInterface.SYSTEM_CACHE;
    Socket[] sockets = new Socket[connections.length];
    ObjectBroker[] brokers = new ObjectBroker[connections.length];

    for (int i = 0; i < connections.length; i++) {
      try {
        sockets[i] = ConnectionFactory.connect(connections[i]);
        brokers[i] = new ObjectBroker(sockets[i]);
        if (LOG.isLoggable(Level.FINE)) {
          StringBuffer log = new StringBuffer();
          log.append("RPC for addLegalHold to ");
          connections[i].toString(log);
          LOG.fine(log.toString());
        }
        brokers[i].launchAddLegalHold(sysCacheId, oid, legalHold, connections[i].getDisks());
      } catch (ConnectException e) {
        LOG.warning(
            "Failed to update the legal hold on ["
                + connections[i]
                + "] for oid "
                + oid
                + " [cache "
                + sysCacheId
                + "]. Error is ["
                + e.getMessage()
                + "]");

        String str = BundleAccess.getInstance().getBundle().getString("err.emd.addLegalHold.io");
        Object[] args = {connections[i].toString()};
        LOG.log(ExtLevel.EXT_SEVERE, MessageFormat.format(str, args));

        if (sockets[i] != null) {
          try {
            sockets[i].close();
          } catch (IOException ignored) {
          }
          sockets[i] = null;
        }
        brokers[i] = null;
      } catch (IOException e) {
        LOG.log(
            Level.SEVERE,
            "Failed to add a legal hold on ["
                + connections[i]
                + "] for oid "
                + oid
                + " [cache "
                + sysCacheId
                + "]",
            e);

        String str = BundleAccess.getInstance().getBundle().getString("err.emd.addLegalHold.io");
        Object[] args = {connections[i].toString()};
        LOG.log(ExtLevel.EXT_SEVERE, MessageFormat.format(str, args));

        if (sockets[i] != null) {
          try {
            sockets[i].close();
          } catch (IOException ignored) {
          }
          sockets[i] = null;
        }
        brokers[i] = null;
      } catch (EMDException e) {
        LOG.log(
            Level.SEVERE,
            "Failed to update the metadata on ["
                + connections[i]
                + "] for oid "
                + oid
                + " [cache "
                + sysCacheId
                + "]",
            e);
        String str = BundleAccess.getInstance().getBundle().getString("err.emd.addLegalHold.cache");
        Object[] args = {connections[i].toString()};
        LOG.log(ExtLevel.EXT_SEVERE, MessageFormat.format(str, args));

        if (sockets[i] != null) {
          try {
            sockets[i].close();
          } catch (IOException ignored) {
          }
          sockets[i] = null;
        }
        brokers[i] = null;
      }
    }

    for (int i = 0; i < connections.length; i++) {
      try {
        if (brokers[i] != null) {
          brokers[i].waitForCompletion();
        }
      } catch (EMDException e) {
        LOG.log(
            Level.SEVERE,
            "The addLegalHold operation didn't return "
                + "properly from ["
                + connections[i]
                + "] for oid "
                + oid
                + " [cache "
                + sysCacheId
                + "]",
            e);

        String str = BundleAccess.getInstance().getBundle().getString("err.emd.addLegalHold.cache");
        Object[] args = {connections[i].toString()};
        LOG.log(ExtLevel.EXT_SEVERE, MessageFormat.format(str, args));

      } finally {
        if (sockets[i] != null) {
          try {
            sockets[i].close();
          } catch (IOException ignored) {
          }
        }
      }
    }
  }
Exemplo n.º 20
0
  private int setMetadata(
      String cacheId,
      ConnectionFactory.DiskConnection[] connections,
      NewObjectIdentifier oid,
      Object argument) {

    Socket[] sockets = new Socket[connections.length];
    ObjectBroker[] brokers = new ObjectBroker[connections.length];

    int inserts = 0;

    for (int i = 0; i < connections.length; i++) {
      try {
        sockets[i] = ConnectionFactory.connect(connections[i]);
        brokers[i] = new ObjectBroker(sockets[i]);
        if (LOG.isLoggable(Level.FINE)) {
          StringBuffer log = new StringBuffer();
          log.append("RPC for setMetadata to ");
          connections[i].toString(log);
          LOG.fine(log.toString());
        }
        brokers[i].launchSetMetadata(cacheId, oid, argument, connections[i].getDisks());
      } catch (ConnectException e) {
        LOG.warning(
            "Failed to update the metadata on ["
                + connections[i]
                + "] for oid "
                + oid
                + " [cache "
                + cacheId
                + "]. Error is ["
                + e.getMessage()
                + "]");

        String str = BundleAccess.getInstance().getBundle().getString("err.emd.setMetadata.io");
        Object[] args = {connections[i].toString()};
        LOG.log(ExtLevel.EXT_SEVERE, MessageFormat.format(str, args));

        if (sockets[i] != null) {
          try {
            sockets[i].close();
          } catch (IOException ignored) {
          }
          sockets[i] = null;
        }
        brokers[i] = null;
      } catch (IOException e) {
        LOG.log(
            Level.SEVERE,
            "Failed to update the metadata on ["
                + connections[i]
                + "] for oid "
                + oid
                + " [cache "
                + cacheId
                + "]",
            e);

        String str = BundleAccess.getInstance().getBundle().getString("err.emd.setMetadata.io");
        Object[] args = {connections[i].toString()};
        LOG.log(ExtLevel.EXT_SEVERE, MessageFormat.format(str, args));

        if (sockets[i] != null) {
          try {
            sockets[i].close();
          } catch (IOException ignored) {
          }
          sockets[i] = null;
        }
        brokers[i] = null;
      } catch (EMDException e) {
        LOG.log(
            Level.SEVERE,
            "Failed to update the metadata on ["
                + connections[i]
                + "] for oid "
                + oid
                + " [cache "
                + cacheId
                + "]",
            e);
        String str = BundleAccess.getInstance().getBundle().getString("err.emd.setMetadata.cache");
        Object[] args = {connections[i].toString()};
        LOG.log(ExtLevel.EXT_SEVERE, MessageFormat.format(str, args));

        if (sockets[i] != null) {
          try {
            sockets[i].close();
          } catch (IOException ignored) {
          }
          sockets[i] = null;
        }
        brokers[i] = null;
      }
    }

    for (int i = 0; i < connections.length; i++) {
      try {
        if (brokers[i] != null) {
          brokers[i].waitForCompletion();
          inserts++;
        }
      } catch (EMDException e) {
        LOG.log(
            Level.SEVERE,
            "The setMetadata operation didn't return properly from ["
                + connections[i]
                + "] for oid "
                + oid
                + " [cache "
                + cacheId
                + "]",
            e);

        String str = BundleAccess.getInstance().getBundle().getString("err.emd.setMetadata.cache");
        Object[] args = {connections[i].toString()};
        LOG.log(ExtLevel.EXT_SEVERE, MessageFormat.format(str, args));

      } finally {
        if (sockets[i] != null) {
          try {
            sockets[i].close();
          } catch (IOException ignored) {
          }
        }
      }
    }

    if (LOG.isLoggable(Level.FINE)) {
      LOG.fine("Metadata insert succeeded with " + inserts + " caches successfully updated.");
    }
    return inserts;
  }
Exemplo n.º 21
0
  /**
   * This the main method of this servlet which takes in the request opens a URLConnection to the
   * CDCServlet endpoint in the OpenAM, and tunnels the request content to it. It parses the
   * Response received and if the HTTP_STATUS is "HTTP_OK" or "HTTP_MOVED_TEMP" POSTs the received
   * Liberty Authn Response to the goto URL specified in the original request.
   */
  private void sendAuthnRequest(
      HttpServletRequest request, HttpServletResponse response, SSOToken token)
      throws ServletException, IOException {
    SessionID sessid = new SessionID(request);
    URL CDCServletURL = null;
    URL sessionServiceURL = null;
    try {
      sessionServiceURL = Session.getSessionServiceURL(sessid);
    } catch (SessionException se) {
      debug.error(
          "CDCClientServlet.sendAuthnRequest: Cannot locate" + " OpenAM instance to forward to.",
          se);
      showError(response, "Cannot locate OpenAM instance to forward to");
    }
    if (sessionServiceURL == null) {
      showError(response, "Cannot locate OpenAM instance to forward to");
    }
    // replace "sessionservice" by cdcservlet in obtained URL
    // we use naming so that we get the URL of the exact server
    // where the session is located and get the right deployment
    // descriptor.
    String sessionServiceURLString = sessionServiceURL.toString();
    int serviceNameIndex =
        sessionServiceURLString.lastIndexOf(
            "/", sessionServiceURLString.length() - 2); // avoiding trailing "/"
    // if any
    StringBuilder buffer = new StringBuilder(150);
    buffer
        .append(sessionServiceURLString.substring(0, serviceNameIndex))
        .append(CDCURI)
        .append(QUESTION_MARK)
        .append(request.getQueryString()); // add query string to
    // CDCServletURL

    CDCServletURL = new URL(buffer.toString());

    // save the go to URL of the agent side to ultimately
    // POST to.
    try {
      HttpURLConnection connection = HttpURLConnectionManager.getConnection(CDCServletURL);
      connection.setRequestMethod("GET");
      connection.setRequestProperty("Content-Type", "text/html;charset=UTF-8");
      connection.setDoOutput(true);
      connection.setUseCaches(false);
      // replay cookies
      String strCookies = getCookiesFromRequest(request);
      if (strCookies != null) {
        if (debug.messageEnabled()) {
          debug.message("CDCClientServlet.sendAuthnRequest:Setting " + "cookies = " + strCookies);
        }
        connection.setRequestProperty("Cookie", strCookies);
      }
      // dont wish to follow redirect to agent, since
      // the response needs to go via the CDCClientServlet.
      HttpURLConnection.setFollowRedirects(false);

      // Receiving input from CDCServlet on the AM server instance
      if (debug.messageEnabled()) {
        debug.message(
            "CDCClientServlet.sendAuthnRequest:Getting " + "response back from  " + CDCServletURL);
        debug.message(
            "CDCClientServlet.sendAuthnRequest:Response " + "Code " + connection.getResponseCode());
        debug.message(
            "CDCClientServlet.sendAuthnRequest:Response "
                + "Message= "
                + connection.getResponseMessage());
      }

      // Check response code
      if ((connection.getResponseCode() == HttpURLConnection.HTTP_OK)
          || (connection.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP)) {
        /**
         * Read the response back from CDCServlet, got a redirect since this response contains the
         * "LARES" ( Liberty authn response, which needs to be posted back to the dest url (agent).
         */
        StringBuilder inBuf = new StringBuilder();
        BufferedReader in =
            new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
        int len;
        char[] buf = new char[1024];
        while ((len = in.read(buf, 0, buf.length)) != -1) {
          inBuf.append(buf, 0, len);
        }
        String inString = inBuf.toString();
        if (debug.messageEnabled()) {
          debug.message(
              "CDCClientServlet.sendAuthnRequest:" + "Received response data = " + inString);
        }
        // put the received Liberty Auth Response
        // in the servlet's response.
        sendAuthnResponse(request, response, inString);
      } else {
        debug.error("CDCClientServlet.sendAuthnRequest: Response " + "code NOT OK/MOVED_TEMP ");
        showError(
            response,
            "ERROR: Received HTTP error code "
                + connection.getResponseCode()
                + " from "
                + CDCServletURL);
      }
    } catch (ConnectException ce) {
      // Debug the exception
      if (debug.warningEnabled()) {
        debug.warning(
            "CDCClientServlet.sendAuthnRequest: " + "Connection Exception to " + CDCServletURL, ce);
      }
      showError(
          response, "Could not connect to CDCServlet at " + CDCServletURL + ":" + ce.getMessage());
    }
  }
Exemplo n.º 22
0
  /**
   * Makes an HTTP connection to a server.
   *
   * @param url The URL of the server to connect to.
   * @param resource The identifier of the resource on the server to address.
   * @param method The name of the HTTP method. Expected to be "GET", "PUT", "POST", or "DELETE".
   * @param request The message body of the request to send. The object is expected to be an
   *     instance of an NGSI 9 or 10 message body.
   * @param contentType The content type that is announced in the request header. The request object
   *     in the message body will be sent in XML format for contentType "application/xml" and JSON
   *     format otherwise.
   * @param xAuthToken The security token used by this connection in order to connect to a component
   *     secured by the FIWARE security mechanisms.
   * @return Either returns the response body returned by the server (as a String) or an error
   *     message.
   */
  public String initializeConnection(
      URL url,
      String resource,
      String method,
      Object request,
      String contentType,
      String xAuthToken) {

    // initialize variables
    HttpURLConnection connection = null;
    InputStream is = null;
    OutputStream os = null;
    String resp = null;

    try {

      // use the above setConnection method to get a connection from url,
      // resource and the method.
      connection = createConnection(url, resource, method, contentType, xAuthToken);

      // get the OutputStram form the connection
      os = connection.getOutputStream();

      if (contentType.equals("application/xml")) {
        // connect using XML message body

        // logger.info("URL" + url + resource);

        logger.info("Starting connection with: " + url + resource);

        // logger.info("Send the QUERY!");

        if (request instanceof NgsiStructure) {
          String string = ((NgsiStructure) request).toString();
          os.write(string.getBytes(Charset.forName("UTF-8")));
        } else {

          // create a context from the request class
          JAXBContext requestContext = JAXBContext.newInstance(request.getClass());

          // Create a Marshaller from the context
          Marshaller m = requestContext.createMarshaller();

          // Ask the marshaller to marshall the request for you
          // logger.info("Request Class: "
          // + request.getClass().toString());
          m.marshal(request, os);
        }

      } else {
        // connect using JSON message body

        logger.info("Starting connection with: " + url + resource);

        // get the OutputStram form the connection
        os = connection.getOutputStream();

        if (request instanceof NgsiStructure) {
          String string = ((NgsiStructure) request).toJsonString();
          os.write(string.getBytes(Charset.forName("UTF-8")));
        } else {

          ObjectMapper mapper = new ObjectMapper();
          SerializationConfig config = mapper.getSerializationConfig();
          config.setSerializationInclusion(Inclusion.NON_NULL);
          // m.setProperty(Marshaller.JAXB_ENCODING, "Unicode");

          try {
            logger.info("----------------->" + mapper.writeValueAsString(request));
            mapper.writeValue(os, request);
          } catch (JsonGenerationException e) {
            if (logger.isDebugEnabled()) {
              logger.debug("JsonGenerationException", e);
            }
          } catch (JsonMappingException e) {
            if (logger.isDebugEnabled()) {
              logger.debug("JsonMappingException", e);
            }
          } catch (IOException e) {
            if (logger.isDebugEnabled()) {
              logger.debug("IOException", e);
            }
          }
        }
      }
      logger.info("Output Stream to " + url + resource + " : " + os.toString());

      // send Message
      os.flush();
      // close connection again
      os.close();

      // now it is time to receive the response
      // get input stream from the connection
      is = connection.getInputStream();

      StringWriter writer = new StringWriter();
      IOUtils.copy(is, writer, "UTF-8");
      resp = writer.toString();

      is.close();

      logger.info("------------->Response = " + resp);

      if (connection.getResponseCode() == 415) {

        logger.info("Connection Error: Format not supported by " + url);

        return "415";
      }

      return resp;

    } catch (ConnectException e) {

      logger.info(
          "IOException: Impossible to establish a connection with " + url + ":" + e.getMessage());
      if (logger.isDebugEnabled()) {
        logger.debug("ConnectException", e);
      }

      return "500 - Connection Error - the URL: " + url + " create an internal error!";

    } catch (JAXBException e) {
      logger.info("XML Parse Error!", e);
      logger.debug("JAXBException", e);
      return "500 - XML Parse Error! Response from: " + url + " is not correct!";
    } catch (IOException e) {

      try {
        if (connection != null && connection.getResponseCode() == 415) {

          logger.info("Connection Error: Format not supported by " + url);

          return "415";
        }
      } catch (IOException e1) {
        if (logger.isDebugEnabled()) {
          logger.debug("IOException", e);
        }
      }

      logger.info("500 - Error I/O with: " + url);
      logger.info(
          "IOException: Impossible to establish a connection with " + url + ": " + e.getMessage());

      return "500 - Error I/O with: " + url;

    } finally {

      if (connection != null) {

        connection.disconnect();
      }
      logger.info("Connection Closed!");
    }
  }
  protected void doPost(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {

    res.setContentType("text/html");
    try {
      if (config == null) {
        config = new PropertiesConfiguration("pnengine.properties");
      }
    } catch (ConfigurationException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }
    String postVariable = config.getString("pnengine.post_variable");
    String engineURL = config.getString("pnengine.url") + "/petrinets";
    String engineURL_host = config.getString("pnengine.url");
    String modelURL = config.getString("pnengine.default_model_url");
    String formURL = null;
    String bindingsURL = null;

    String rdf = req.getParameter("data");
    String diagramTitle = req.getParameter("title");

    DocumentBuilder builder;
    BPMNDiagram diagram;
    try {
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      builder = factory.newDocumentBuilder();
      Document document = builder.parse(new ByteArrayInputStream(rdf.getBytes()));
      BPMNRDFImporter importer = new BPMNRDFImporter(document);
      diagram = (BPMNDiagram) importer.loadBPMN();

      String basefilename = String.valueOf(System.currentTimeMillis());
      String tmpPNMLFile =
          this.getServletContext().getRealPath("/")
              + "tmp"
              + File.separator
              + basefilename
              + ".pnml";
      BufferedWriter out1 = new BufferedWriter(new FileWriter(tmpPNMLFile));

      // URL only for testing purposes...
      ExecConverter converter =
          new ExecConverter(diagram, modelURL, this.getServletContext().getRealPath("/"));
      converter.setBaseFileName(
          this.getServletContext().getRealPath("/") + "tmp" + File.separator + basefilename);
      PetriNet net = converter.convert();
      ExecPetriNet execnet = (ExecPetriNet) net;
      Document pnmlDoc = builder.newDocument();

      ExecPNPNMLExporter exp = new ExecPNPNMLExporter();
      execnet.setName(diagramTitle);
      exp.savePetriNet(pnmlDoc, execnet);

      OutputFormat format = new OutputFormat(pnmlDoc);
      XMLSerializer serial = new XMLSerializer(out1, format);
      serial.asDOMSerializer();
      serial.serialize(pnmlDoc.getDocumentElement());
      out1.close();

      StringWriter stringOut = new StringWriter();
      XMLSerializer serial2 = new XMLSerializer(stringOut, format);
      serial2.asDOMSerializer();

      serial2.serialize(pnmlDoc.getDocumentElement());

      URL url_engine = new URL(engineURL);
      HttpURLConnection connection_engine = (HttpURLConnection) url_engine.openConnection();
      connection_engine.setRequestMethod("POST");

      String encoding = new sun.misc.BASE64Encoder().encode("testuser:"******"Authorization", "Basic " + encoding);

      connection_engine.setUseCaches(false);
      connection_engine.setDoInput(true);
      connection_engine.setDoOutput(true);

      String escaped_content =
          postVariable + "=" + URLEncoder.encode(stringOut.toString(), "UTF-8");

      connection_engine.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
      connection_engine.setRequestProperty(
          "Accept",
          "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");

      // connection_engine.setRequestProperty("Content-Length",
      // ""+escaped_content.getBytes().length);
      String errorMessage = null;
      try {
        connection_engine.getOutputStream().write(escaped_content.getBytes());
        connection_engine.connect();
      } catch (ConnectException e) {
        errorMessage = e.getMessage();
      }

      // output link address
      res.getWriter().print("tmp/" + basefilename + ".pnml" + "\">View PNML</a><br/><br/>");
      if (errorMessage == null && connection_engine.getResponseCode() == 200) {
        res.getWriter()
            .println(
                "Deployment to Engine <a href=\""
                    + engineURL_host
                    + "/worklist\" target=\"_blank\">"
                    + engineURL_host
                    + "</a><br/><b>successful</b>!");
      } else {
        res.getWriter()
            .println(
                "Deployment to Engine <a href=\""
                    + engineURL
                    + "\" target=\"_blank\">"
                    + engineURL
                    + "</a><br/><b>failed</b> with message: \n"
                    + errorMessage
                    + "!");
      }

    } catch (ParserConfigurationException e1) {
      res.getWriter().println(e1.getMessage());
    } catch (SAXException e1) {
      res.getWriter().println(e1.getMessage());
    }
  }
Exemplo n.º 24
0
  protected boolean execStreamConnect(Properties props) {
    if (props == null) {
      _log.debug("No parameters specified in STREAM CONNECT message");
      return false;
    }

    int id;
    {
      String strid = props.getProperty("ID");
      if (strid == null) {
        _log.debug("ID not specified in STREAM SEND message");
        return false;
      }
      try {
        id = Integer.parseInt(strid);
      } catch (NumberFormatException e) {
        _log.debug("Invalid STREAM CONNECT ID specified: " + strid);
        return false;
      }
      if (id < 1) {
        _log.debug("Invalid STREAM CONNECT ID specified: " + strid);
        return false;
      }
      props.remove("ID");
    }

    String dest = props.getProperty("DESTINATION");
    if (dest == null) {
      _log.debug("Destination not specified in RAW SEND message");
      return false;
    }
    props.remove("DESTINATION");

    try {
      try {
        if (!getStreamSession().connect(id, dest, props)) {
          _log.debug("STREAM connection failed");
          return false;
        }
      } catch (DataFormatException e) {
        _log.debug("Invalid destination in STREAM CONNECT message");
        notifyStreamOutgoingConnection(id, "INVALID_KEY", null);
      } catch (SAMInvalidDirectionException e) {
        _log.debug("STREAM CONNECT failed: " + e.getMessage());
        notifyStreamOutgoingConnection(id, "INVALID_DIRECTION", null);
      } catch (ConnectException e) {
        _log.debug("STREAM CONNECT failed: " + e.getMessage());
        notifyStreamOutgoingConnection(id, "CONNECTION_REFUSED", null);
      } catch (NoRouteToHostException e) {
        _log.debug("STREAM CONNECT failed: " + e.getMessage());
        notifyStreamOutgoingConnection(id, "CANT_REACH_PEER", null);
      } catch (InterruptedIOException e) {
        _log.debug("STREAM CONNECT failed: " + e.getMessage());
        notifyStreamOutgoingConnection(id, "TIMEOUT", null);
      } catch (I2PException e) {
        _log.debug("STREAM CONNECT failed: " + e.getMessage());
        notifyStreamOutgoingConnection(id, "I2P_ERROR", null);
      }
    } catch (IOException e) {
      return false;
    }

    return true;
  }
Exemplo n.º 25
0
  public String initializeUpdateContextConnectionToOrion(
      URL url, String resource, UpdateContextRequest request, String xAuthToken) {

    // initialize variables
    HttpURLConnection connection = null;
    InputStream is = null;
    OutputStream os = null;
    String resp = null;

    // Orion is accepting a non-NGSI standard updateContext e.g.
    /*
     * { "contextElements": [ { "type": "Room", "isPattern": "false", "id":
     * "Room1", "attributes": [ { "name": "temperature", "type": "float",
     * "value": "23" }, { "name": "pressure", "type": "integer", "value":
     * "720" } ] } ], "updateAction": "APPEND" }
     */

    // List<ContextElement> contextElementsForOrion = new
    // ArrayList<ContextElement>();
    // for (ContextElement contextElement : request.getContextElement()) {
    //
    // // List<ContextAttribute> contextAttributesForOrion = new
    // // ArrayList<ContextAttribute>();
    // // for (ContextAttribute contextAttribute : contextElement
    // // .getContextAttributeList()) {
    // //
    // // ContextAttribute contextAttributeForOrion = new ContextAttribute(
    // // contextAttribute.getName(), contextAttribute.getType(),
    // // contextAttribute.getContextValue());
    // // contextAttributesForOrion.add(contextAttributeForOrion);
    // //
    // // }
    //
    // ContextElement contextElementForOrion = new ContextElement(
    // contextElement.getEntityId(), null,
    // contextElement.getContextAttributeList(), null);
    // contextElementsForOrion.add(contextElementForOrion);
    // }
    // UpdateContextRequest orionUpdateContextRequest = new
    // UpdateContextRequest(
    // contextElementsForOrion, request.getUpdateAction());

    UpdateContextRequest_OrionCustomization orionUpdateContextRequest =
        new UpdateContextRequest_OrionCustomization(request);
    logger.info(
        "Translating UpdateContextRequest :"
            + request.toJsonString()
            + " to the Orion Customization:"
            + orionUpdateContextRequest.toJsonString());

    try {

      // use the above setConnection method to get a connection from url,
      // resource and the method.
      connection = createConnection(url, resource, "POST", "application/json", xAuthToken);

      // get the OutputStram form the connection
      os = connection.getOutputStream();

      // connect using JSON message body

      // get the OutputStram form the connection
      os = connection.getOutputStream();

      String string = ((NgsiStructure) orionUpdateContextRequest).toJsonString();

      // string = string.replaceAll("contextValue", "value");

      // // The first is for deleting if it is followed by a comma (other
      // // fields next to it)
      // string = string.replaceAll(".attributeDomainName.:\\[.*?\\],",
      // "");
      // // The second is for deleting if it is followed by the bracket
      // (end
      // // of the structure)
      // string = string.replaceAll(".attributeDomainName.:\\[.*?\\]}",
      // "}");
      //
      // // The first is for deleting if it is followed by a comma (other
      // // fields next to it)
      // string = string.replaceAll(".domainMetadata.:\\[.*?\\],", "");
      // // The second is for deleting if it is followed by the bracket
      // (end
      // // of the structure)
      // string = string.replaceAll(".domainMetadata.:\\[.*?\\]}", "}");
      //
      // // The first is for deleting if it is followed by a comma (other
      // // fields next to it)
      // string = string.replaceAll("\"metadata\"", "\"metadatas\"");
      //
      // string = this.levelUpEntityId(string);

      os.write(string.getBytes(Charset.forName("UTF-8")));

      logger.info("Output Stream to " + url + resource + " : " + os.toString());

      // send Message
      os.flush();
      // close connection again
      os.close();

      // now it is time to receive the response
      // get input stream from the connection
      is = connection.getInputStream();

      StringWriter writer = new StringWriter();
      IOUtils.copy(is, writer, "UTF-8");
      resp = writer.toString();

      is.close();

      logger.info("Response from " + url + resource + " : " + resp);

      if (connection.getResponseCode() == 415) {

        logger.info("Connection Error: Format not supported by " + url);

        return "415";
      }

      UpdateContextResponse_OrionCustomization updateContextResponse_OrionCustomization =
          (UpdateContextResponse_OrionCustomization)
              NgsiStructure.parseStringToJson(resp, UpdateContextResponse_OrionCustomization.class);

      return updateContextResponse_OrionCustomization.toUpdateContextResponse().toJsonString();

    } catch (ConnectException e) {

      logger.info(
          "IOException: Impossible to establish a connection with " + url + ":" + e.getMessage());
      if (logger.isDebugEnabled()) {
        logger.debug("ConnectException", e);
      }

      return "500 - Connection Error - the URL: " + url + " cannot be reached!";

    } catch (IOException e) {

      try {
        if (connection != null && connection.getResponseCode() == 415) {

          logger.info("Connection Error: Format not supported by " + url);

          return "415";
        }
      } catch (IOException e1) {
        if (logger.isDebugEnabled()) {
          logger.debug("IOException", e);
        }
      }

      logger.info("500 - Error I/O with: " + url);
      logger.info(
          "IOException: Impossible to establish a connection with " + url + ":" + e.getMessage());

      return "500 - Error I/O with: " + url;

    } finally {

      if (connection != null) {

        connection.disconnect();
      }
      logger.info("Connection Closed!");
    }
  }
Exemplo n.º 26
0
  /**
   * @param theUrl
   * @param conAttempts
   * @return
   * @throws IOException
   */
  public boolean validUrl(String theUrl, int conAttempts) throws IOException {
    long total_time = 0, endTime = 0;
    long startTime = System.currentTimeMillis();
    URL link = new URL(theUrl);
    int CONNECT_TIMEOUT = 5000, READ_TIMEOUT = 2000;

    HttpURLConnection huc = (HttpURLConnection) link.openConnection();
    huc.setRequestProperty("User-Agent", userAgent);
    huc.setConnectTimeout(CONNECT_TIMEOUT);
    huc.setReadTimeout(READ_TIMEOUT);
    try {
      huc.connect();

    } catch (java.net.ConnectException e) {
      print(e.getMessage() + "\n");
      if (e.getMessage().equalsIgnoreCase("Connection timed out")) {
        if (conAttempts != MAX_CONNECTION_ATTEMPTS) {
          System.out.println("Recurrencing validUrl method...");
          return validUrl(theUrl, conAttempts + 1);
        } else return false;
      } else return false;

    } catch (java.net.SocketTimeoutException e) {
      print(e.getMessage() + "\n");
      if (e.getMessage().equalsIgnoreCase("connect timed out")) {
        if (conAttempts != MAX_CONNECTION_ATTEMPTS) {
          System.out.println("Recurrencing validUrl method...");
          return validUrl(theUrl, conAttempts + 1);
        } else return false;
      } else return false;

    } catch (IOException e) {
      print(e.getMessage() + "\n");
      return false;
    }
    UrlValidator urlValidator = new UrlValidator();
    if (urlValidator.isValid(theUrl) == true) {
      System.out.println("valid url form");
      if (huc.getContentType() != null) {
        System.out.println("Content: " + huc.getContentType());
        if (huc.getContentType().equals("text/html")
            || huc.getContentType().equals("unknown/unknown")) {
          if (getResposeCode(theUrl, 0) >= java.net.HttpURLConnection.HTTP_BAD_REQUEST) {
            System.out.println("Server Response Code: " + getResposeCode(theUrl, 0));
            return false;
          }
        }
        System.out.println(huc.getContentType());
        endTime = System.currentTimeMillis();
        total_time = total_time + (endTime - startTime);
        System.out.println("Total elapsed time is :" + total_time + "\n");
        return true;
      } else { // edw erxetai an den prolavei na diavasei h an einai null to content
        endTime = System.currentTimeMillis();
        total_time = total_time + (endTime - startTime);
        System.out.println("Total elapsed time is :" + total_time + "\n");
        if (conAttempts != MAX_CONNECTION_ATTEMPTS) {
          System.out.println("Recurrencing validUrl method...");
          return validUrl(theUrl, conAttempts + 1);
        } else return false;
      }
    } else {
      endTime = System.currentTimeMillis();
      total_time = total_time + (endTime - startTime);
      System.out.println("Total elapsed time is :" + total_time + "\n");
      return false;
    }
  }
Exemplo n.º 27
0
  public static void readURL(String urlString, int bufferSize) throws IOException {

    System.out.println("start=" + new Date());
    long start = System.currentTimeMillis();

    URL url;
    java.io.InputStream is = null;
    try {
      url = new URL(urlString);
    } catch (MalformedURLException e) {
      throw new IOException(
          "** MalformedURLException on URL <" + urlString + ">\n" + e.getMessage() + "\n");
    }

    try {
      java.net.HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
      // check response code is good
      int responseCode = httpConnection.getResponseCode();
      System.out.println(" response code= " + responseCode);
      if (responseCode / 100 != 2)
        throw new IOException(
            "** Cant open URL <"
                + urlString
                + ">\n Response code = "
                + responseCode
                + "\n"
                + httpConnection.getResponseMessage()
                + "\n");

      // read it
      is = httpConnection.getInputStream();

      int nreads = 0;
      long totalB = 0;
      long total = 0;
      byte[] buffer = new byte[bufferSize];
      while (true) {
        int bytesRead = is.read(buffer);
        if (bytesRead == -1) break;
        totalB += bytesRead;
        total += buffer[0]; // prevent compiler optimization
        nreads++;
      }
      double took = .001 * (System.currentTimeMillis() - start);
      double rate = totalB / took / (1000 * 1000);
      double avg = totalB / nreads;
      System.out.println(
          " readURL ("
              + bufferSize
              + ") took = "
              + took
              + " sec; rate = "
              + rate
              + "Mb/sec avg read= "
              + avg);
      System.out.println("   dummy=" + total);
      System.out.println(" end=" + new Date());

    } catch (java.net.ConnectException e) {
      throw new IOException(
          "** ConnectException on URL: <"
              + urlString
              + ">\n"
              + e.getMessage()
              + "\nServer probably not running");

    } finally {
      if (is != null) is.close();
    }
  }
Exemplo n.º 28
0
  /**
   * Creates a new HTTP stream. If there is a saved connection to the same host, use it.
   *
   * @param path the URL for the stream
   * @return the opened stream
   */
  private static HttpStream createStream(HttpPath path) throws IOException {
    String host = path.getHost();
    int port = path.getPort();

    HttpStream stream = null;
    long streamTime = 0;
    synchronized (LOCK) {
      if (_savedStream != null
          && host.equals(_savedStream.getHost())
          && port == _savedStream.getPort()) {
        stream = _savedStream;
        streamTime = _saveTime;
        _savedStream = null;
      }
    }

    if (stream != null) {
      long now;

      now = CurrentTime.getCurrentTime();

      if (now < streamTime + 5000) {
        // if the stream is still valid, use it
        stream.init(path);
        return stream;
      } else {
        // if the stream has timed out, close it
        try {
          stream._isKeepalive = false;
          stream.close();
        } catch (IOException e) {
          log.log(Level.FINE, e.toString(), e);
        }
      }
    }

    Socket s;

    try {
      s = new Socket(host, port);

      if (path instanceof HttpsPath) {
        SSLContext context = SSLContext.getInstance("TLSv1");

        javax.net.ssl.TrustManager tm =
            new javax.net.ssl.X509TrustManager() {
              public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
              }

              public void checkClientTrusted(
                  java.security.cert.X509Certificate[] cert, String foo) {}

              public void checkServerTrusted(
                  java.security.cert.X509Certificate[] cert, String foo) {}
            };

        context.init(null, new javax.net.ssl.TrustManager[] {tm}, null);
        SSLSocketFactory factory = context.getSocketFactory();

        SSLSocket sslSock = (SSLSocket) factory.createSocket(s, host, port, true);

        s = sslSock;
      }
    } catch (ConnectException e) {
      throw new ConnectException(path.getURL() + ": " + e.getMessage());
    } catch (Exception e) {
      throw new ConnectException(path.getURL() + ": " + e.toString());
    }

    int socketTimeout = 300 * 1000;

    try {
      s.setSoTimeout(socketTimeout);
    } catch (Exception e) {
    }

    return new HttpStream(path, host, port, s);
  }