/* */ private void handleEvent(Event paramEvent) /* */ { /* 266 */ ExchangeImpl localExchangeImpl = paramEvent.exchange; /* 267 */ HttpConnection localHttpConnection = localExchangeImpl.getConnection(); /* */ try { /* 269 */ if ((paramEvent instanceof WriteFinishedEvent)) /* */ { /* 271 */ int i = ServerImpl.this.endExchange(); /* 272 */ if ((ServerImpl.this.terminating) && (i == 0)) { /* 273 */ ServerImpl.this.finished = true; /* */ } /* 275 */ ServerImpl.this.responseCompleted(localHttpConnection); /* 276 */ LeftOverInputStream localLeftOverInputStream = localExchangeImpl.getOriginalInputStream(); /* 277 */ if (!localLeftOverInputStream.isEOF()) { /* 278 */ localExchangeImpl.close = true; /* */ } /* 280 */ if ((localExchangeImpl.close) || (ServerImpl.this.idleConnections.size() >= ServerImpl.MAX_IDLE_CONNECTIONS)) { /* 281 */ localHttpConnection.close(); /* 282 */ ServerImpl.this.allConnections.remove(localHttpConnection); /* */ } /* 284 */ else if (localLeftOverInputStream.isDataBuffered()) /* */ { /* 286 */ ServerImpl.this.requestStarted(localHttpConnection); /* 287 */ handle(localHttpConnection.getChannel(), localHttpConnection); /* */ } else { /* 289 */ this.connsToRegister.add(localHttpConnection); /* */ } /* */ } /* */ } /* */ catch (IOException localIOException) { /* 294 */ ServerImpl.this.logger.log(Level.FINER, "Dispatcher (1)", localIOException); /* */ /* 297 */ localHttpConnection.close(); /* */ } /* */ }
public void close() { if (closed) { return; } closed = true; /* close the underlying connection if, * a) the streams not set up yet, no response can be sent, or * b) if the wrapper output stream is not set up, or * c) if the close of the input/outpu stream fails */ try { if (uis_orig == null || uos == null) { connection.close(); return; } if (!uos_orig.isWrapped()) { connection.close(); return; } if (!uis_orig.isClosed()) { uis_orig.close(); } uos.close(); } catch (IOException e) { connection.close(); } }
/* */ public void stop(int paramInt) { /* 180 */ if (paramInt < 0) { /* 181 */ throw new IllegalArgumentException("negative delay parameter"); /* */ } /* 183 */ this.terminating = true; /* */ try { this.schan.close(); } catch (IOException localIOException) { /* 185 */ }this.selector.wakeup(); /* 186 */ long l = System.currentTimeMillis() + paramInt * 1000; /* 187 */ while (System.currentTimeMillis() < l) { /* 188 */ delay(); /* 189 */ if (this.finished) { /* 190 */ break; /* */ } /* */ } /* 193 */ this.finished = true; /* 194 */ this.selector.wakeup(); /* 195 */ synchronized (this.allConnections) { /* 196 */ for (HttpConnection localHttpConnection : this.allConnections) { /* 197 */ localHttpConnection.close(); /* */ } /* */ } /* 200 */ this.allConnections.clear(); /* 201 */ this.idleConnections.clear(); /* 202 */ this.timer.cancel(); /* 203 */ if (timer1Enabled) /* 204 */ this.timer1.cancel(); /* */ }
private HttpResponse _send() { if (httpConnection == null) { open(); } // sends data HttpResponse httpResponse; try { OutputStream outputStream = httpConnection.getOutputStream(); sendTo(outputStream); InputStream inputStream = httpConnection.getInputStream(); httpResponse = HttpResponse.readFrom(inputStream); httpResponse.assignHttpRequest(this); } catch (IOException ioex) { throw new HttpException(ioex); } boolean keepAlive = httpResponse.isConnectionPersistent(); if (!keepAlive) { // closes connection if keep alive is false, or if counter reached 0 httpConnection.close(); httpConnection = null; } return httpResponse; }
private Image getAvatar() { HttpConnection httemp = null; InputStream istemp = null; Image avatar = null; try { httemp = (HttpConnection) Connector.open(url); if (HttpConnection.HTTP_OK != httemp.getResponseCode()) { throw new IOException(); } istemp = httemp.openInputStream(); byte[] avatarBytes = read(istemp, (int) httemp.getLength()); // #sijapp cond.if modules_TRAFFIC is "true" # Traffic.getInstance().addInTraffic(avatarBytes.length); // #sijapp cond.end# avatar = javax.microedition.lcdui.Image.createImage(avatarBytes, 0, avatarBytes.length); avatarBytes = null; } catch (Exception e) { } try { httemp.close(); istemp.close(); } catch (Exception e) { } return avatar; }
private void shutdown() { // Shutting down the parser may invoke messageComplete() or fail() parser.shutdownInput(); State state = this.state.get(); if (state == State.IDLE || state == State.RECEIVE) { if (!fail(new EOFException())) connection.close(); } }
/** * Processes a new connection making it idle or active depending on whether requests are waiting * to be sent. * * <p>A new connection is created when a request needs to be executed; it is possible that the * request that triggered the request creation is executed by another connection that was just * released, so the new connection may become idle. * * <p>If a request is waiting to be executed, it will be dequeued and executed by the new * connection. * * @param connection the new connection * @param dispatch whether to dispatch the processing to another thread */ protected void process(Connection connection, boolean dispatch) { // Ugly cast, but lack of generic reification forces it final HttpConnection httpConnection = (HttpConnection) connection; final HttpExchange exchange = exchanges.poll(); if (exchange == null) { LOG.debug("{} idle", httpConnection); if (!idleConnections.offer(httpConnection)) { LOG.debug("{} idle overflow"); httpConnection.close(); } if (!client.isRunning()) { LOG.debug("{} is stopping", client); remove(httpConnection); httpConnection.close(); } } else { final Request request = exchange.getRequest(); Throwable cause = request.getAbortCause(); if (cause != null) { abort(exchange, cause); LOG.debug("Aborted before processing {}: {}", exchange, cause); } else { LOG.debug("{} active", httpConnection); if (!activeConnections.offer(httpConnection)) { LOG.warn("{} active overflow"); } if (dispatch) { client .getExecutor() .execute( new Runnable() { @Override public void run() { httpConnection.send(exchange); } }); } else { httpConnection.send(exchange); } } } }
/* */ private void closeConnection(HttpConnection paramHttpConnection) { /* 455 */ paramHttpConnection.close(); /* 456 */ this.allConnections.remove(paramHttpConnection); /* 457 */ switch (1.$SwitchMap$sun$net$httpserver$HttpConnection$State[paramHttpConnection.getState().ordinal()]) { /* */ case 1: /* 459 */ this.reqConnections.remove(paramHttpConnection); /* 460 */ break; /* */ case 2: /* 462 */ this.rspConnections.remove(paramHttpConnection); /* 463 */ break; /* */ case 3: /* 465 */ this.idleConnections.remove(paramHttpConnection);
private Object request( String url, boolean post, Hashtable params, boolean basicAuth, boolean processOutput) throws Exception { HttpConnection conn = null; Writer writer = null; InputStream is = null; try { if (!post && (params != null)) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStreamWriter osw = new OutputStreamWriter(baos, this.encoding); this.encodeParams(params, osw); osw.flush(); osw.close(); osw = null; url += "?" + new String(baos.toByteArray(), this.encoding); baos = null; } conn = (HttpConnection) Connector.open(url); conn.setRequestMethod(post ? HttpConnection.POST : HttpConnection.GET); if (basicAuth) { if (!this.areCredentialsSet()) throw new Exception("Credentials are not set"); String token = base64Encode((this.user + ":" + this.pass).getBytes(this.encoding)); conn.setRequestProperty("Authorization", "Basic " + token); } if (post && (params != null)) { OutputStream os = conn.openOutputStream(); writer = new OutputStreamWriter(os, this.encoding); this.encodeParams(params, writer); writer.flush(); writer.close(); os = null; writer = null; } int code = conn.getResponseCode(); if ((code != 200) && (code != 302)) throw new Exception("Unexpected response code " + code + ": " + conn.getResponseMessage()); is = conn.openInputStream(); if (processOutput) { synchronized (this.json) { return this.json.parse(is); } } else { this.pump(is, System.out, 1024); return null; } } finally { if (writer != null) writer.close(); if (is != null) is.close(); if (conn != null) conn.close(); } }
/* */ public void run() { /* 820 */ LinkedList localLinkedList = new LinkedList(); /* 821 */ ServerImpl.this.time = System.currentTimeMillis(); /* */ Iterator localIterator; /* */ HttpConnection localHttpConnection; /* 822 */ synchronized (ServerImpl.this.reqConnections) { /* 823 */ if (ServerImpl.MAX_REQ_TIME != -1L) { /* 824 */ for (localIterator = ServerImpl.this.reqConnections.iterator(); localIterator.hasNext(); ) { localHttpConnection = (HttpConnection)localIterator.next(); /* 825 */ if (localHttpConnection.creationTime + ServerImpl.TIMER_MILLIS + ServerImpl.MAX_REQ_TIME <= ServerImpl.this.time) { /* 826 */ localLinkedList.add(localHttpConnection); /* */ } /* */ } /* 829 */ for (localIterator = localLinkedList.iterator(); localIterator.hasNext(); ) { localHttpConnection = (HttpConnection)localIterator.next(); /* 830 */ ServerImpl.this.logger.log(Level.FINE, "closing: no request: " + localHttpConnection); /* 831 */ ServerImpl.this.reqConnections.remove(localHttpConnection); /* 832 */ ServerImpl.this.allConnections.remove(localHttpConnection); /* 833 */ localHttpConnection.close(); /* */ } /* */ } /* */ } /* 837 */ localLinkedList = new LinkedList(); /* 838 */ synchronized (ServerImpl.this.rspConnections) { /* 839 */ if (ServerImpl.MAX_RSP_TIME != -1L) { /* 840 */ for (localIterator = ServerImpl.this.rspConnections.iterator(); localIterator.hasNext(); ) { localHttpConnection = (HttpConnection)localIterator.next(); /* 841 */ if (localHttpConnection.rspStartedTime + ServerImpl.TIMER_MILLIS + ServerImpl.MAX_RSP_TIME <= ServerImpl.this.time) { /* 842 */ localLinkedList.add(localHttpConnection); /* */ } /* */ } /* 845 */ for (localIterator = localLinkedList.iterator(); localIterator.hasNext(); ) { localHttpConnection = (HttpConnection)localIterator.next(); /* 846 */ ServerImpl.this.logger.log(Level.FINE, "closing: no response: " + localHttpConnection); /* 847 */ ServerImpl.this.rspConnections.remove(localHttpConnection); /* 848 */ ServerImpl.this.allConnections.remove(localHttpConnection); /* 849 */ localHttpConnection.close(); /* */ } /* */ } /* */ } /* */ }
/* */ void reRegister(HttpConnection paramHttpConnection) /* */ { /* */ try /* */ { /* 307 */ SocketChannel localSocketChannel = paramHttpConnection.getChannel(); /* 308 */ localSocketChannel.configureBlocking(false); /* 309 */ SelectionKey localSelectionKey = localSocketChannel.register(ServerImpl.this.selector, 1); /* 310 */ localSelectionKey.attach(paramHttpConnection); /* 311 */ paramHttpConnection.selectionKey = localSelectionKey; /* 312 */ paramHttpConnection.time = (ServerImpl.this.getTime() + ServerImpl.IDLE_INTERVAL); /* 313 */ ServerImpl.this.idleConnections.add(paramHttpConnection); /* */ } catch (IOException localIOException) { /* 315 */ ServerImpl.dprint(localIOException); /* 316 */ ServerImpl.this.logger.log(Level.FINER, "Dispatcher(8)", localIOException); /* 317 */ paramHttpConnection.close(); /* */ } /* */ }
/* */ public void run() /* */ { /* 798 */ LinkedList localLinkedList = new LinkedList(); /* 799 */ ServerImpl.this.time = System.currentTimeMillis(); /* 800 */ ServerImpl.access$1808(ServerImpl.this); /* */ Iterator localIterator; /* */ HttpConnection localHttpConnection; /* 801 */ synchronized (ServerImpl.this.idleConnections) { /* 802 */ for (localIterator = ServerImpl.this.idleConnections.iterator(); localIterator.hasNext(); ) { localHttpConnection = (HttpConnection)localIterator.next(); /* 803 */ if (localHttpConnection.time <= ServerImpl.this.time) { /* 804 */ localLinkedList.add(localHttpConnection); /* */ } /* */ } /* 807 */ for (localIterator = localLinkedList.iterator(); localIterator.hasNext(); ) { localHttpConnection = (HttpConnection)localIterator.next(); /* 808 */ ServerImpl.this.idleConnections.remove(localHttpConnection); /* 809 */ ServerImpl.this.allConnections.remove(localHttpConnection); /* 810 */ localHttpConnection.close(); } /* */ } /* */ }
private String getContent(String url) { HttpConnection httemp = null; InputStream istemp = null; String content = ""; try { httemp = (HttpConnection) Connector.open(url); httemp.setRequestProperty("Connection", "cl" + "ose"); if (HttpConnection.HTTP_OK != httemp.getResponseCode()) { throw new IOException(); } istemp = httemp.openInputStream(); int length = (int) httemp.getLength(); if (-1 != length) { byte[] bytes = new byte[length]; istemp.read(bytes); content = new String(bytes); } else { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); while (true) { int ch = istemp.read(); if (-1 == ch) break; bytes.write(ch); } content = new String(bytes.toByteArray()); bytes.close(); } } catch (Exception e) { content = "Error: " + e.getMessage(); } try { httemp.close(); istemp.close(); } catch (Exception e) { } return StringConvertor.removeCr(content); }
private void failAndClose(Throwable failure) { fail(failure); connection.close(); }
public InputStream makeRequest(String url, OutputStream pos) throws IOException { HttpConnection conn = null; ByteArrayOutputStream bos = null; DataInputStream is = null; DataOutputStream os = null; InputStream pis = null; try { conn = (HttpConnection) Connector.open(url, Connector.READ_WRITE); conn.setRequestMethod(HttpConnection.POST); conn.setRequestProperty("Content-Type", "application/octet-stream"); conn.setRequestProperty("Accept", "application/octet-stream"); if (sessionCookie == null) { conn.setRequestProperty("version", "???"); } else { conn.setRequestProperty("cookie", sessionCookie); } // Getting the output stream may flush the headers os = conn.openDataOutputStream(); os.write(pos.getBytes()); os.close(); int responseCode; try { responseCode = conn.getResponseCode(); } catch (IOException e) { throw new IOException("No response from " + url); } if (responseCode != HttpConnection.HTTP_OK) { throw new IllegalArgumentException(); } bos = new ByteArrayOutputStream(); { is = conn.openDataInputStream(); String sc = conn.getHeaderField("set-cookie"); if (sc != null) { sessionCookie = sc; } while (true) { int ch = is.read(); if (ch == -1) break; bos.write(ch); } is.close(); is = null; conn.close(); conn = null; } pis = new InputStream(bos.toByteArray()); bos.close(); bos = null; } catch (Exception e) { e.printStackTrace(); try { if (conn != null) { conn.close(); conn = null; } if (bos != null) { bos.close(); bos = null; } if (is != null) { is.close(); is = null; } } catch (Exception exc) { } throw new IOException(); } return pis; }
/** * READ * * @param url * @return * @throws IOException */ public String get(String url) throws IOException { HttpConnection hcon = null; DataInputStream dis = null; StringBuffer responseMessage = new StringBuffer(); try { int redirectTimes = 0; boolean redirect; do { redirect = false; // a standard HttpConnection with READ access hcon = getConnection(url); // obtain a DataInputStream from the HttpConnection dis = new DataInputStream(hcon.openInputStream()); // retrieve the response from the server int ch; while ((ch = dis.read()) != -1) { responseMessage.append((char) ch); } // end while ( ( ch = dis.read() ) != -1 ) // check status code int status = hcon.getResponseCode(); switch (status) { case HttpConnection.HTTP_OK: // Success! break; case HttpConnection.HTTP_TEMP_REDIRECT: case HttpConnection.HTTP_MOVED_TEMP: case HttpConnection.HTTP_MOVED_PERM: // Redirect: get the new location url = hcon.getHeaderField("location"); System.out.println("Redirect: " + url); if (dis != null) dis.close(); if (hcon != null) hcon.close(); hcon = null; redirectTimes++; redirect = true; break; default: // Error: throw exception hcon.close(); throw new IOException("Response status not OK:" + status); } // max 5 redirects } while (redirect == true && redirectTimes < 5); if (redirectTimes == 5) { throw new IOException("Too much redirects"); } } catch (Exception e) { e.printStackTrace(); // TODO bad style responseMessage.append("ERROR: "); } finally { try { if (hcon != null) hcon.close(); if (dis != null) dis.close(); } catch (IOException ioe) { ioe.printStackTrace(); } // end try/catch } // end try/catch/finally return responseMessage.toString(); } // end sendGetRequest( String )
/** * UPDATE * * @param url * @param data the request body * @throws IOException */ public String post(String url, String data) throws IOException { HttpConnection hcon = null; DataInputStream dis = null; DataOutputStream dos = null; StringBuffer responseMessage = new StringBuffer(); try { int redirectTimes = 0; boolean redirect; do { redirect = false; // an HttpConnection with both read and write access hcon = getConnection(url, Connector.READ_WRITE); // set the request method to POST hcon.setRequestMethod(HttpConnection.POST); // overwrite content type to be form based hcon.setRequestProperty("Content-Type", "application/json"); // set message length if (data != null) { hcon.setRequestProperty("Content-Length", "" + data.length()); } if (data != null) { // obtain DataOutputStream for sending the request string dos = hcon.openDataOutputStream(); byte[] request_body = data.getBytes(); // send request string to server for (int i = 0; i < request_body.length; i++) { dos.writeByte(request_body[i]); } // end for( int i = 0; i < request_body.length; i++ ) dos.flush(); // Including this line may produce // undesiredresults on certain devices } // obtain DataInputStream for receiving server response dis = new DataInputStream(hcon.openInputStream()); // retrieve the response from server int ch; while ((ch = dis.read()) != -1) { responseMessage.append((char) ch); } // end while( ( ch = dis.read() ) != -1 ) { // check status code int status = hcon.getResponseCode(); switch (status) { case HttpConnection.HTTP_OK: // Success! break; case HttpConnection.HTTP_TEMP_REDIRECT: case HttpConnection.HTTP_MOVED_TEMP: case HttpConnection.HTTP_MOVED_PERM: // Redirect: get the new location url = hcon.getHeaderField("location"); System.out.println("Redirect: " + url); if (dis != null) dis.close(); if (hcon != null) hcon.close(); hcon = null; redirectTimes++; redirect = true; break; default: // Error: throw exception hcon.close(); throw new IOException("Response status not OK:" + status); } // max 5 redirects } while (redirect == true && redirectTimes < 5); if (redirectTimes == 5) { throw new IOException("Too much redirects"); } } catch (Exception e) { e.printStackTrace(); responseMessage.append("ERROR"); } finally { // free up i/o streams and http connection try { if (hcon != null) hcon.close(); if (dis != null) dis.close(); if (dos != null) dos.close(); } catch (IOException ioe) { ioe.printStackTrace(); } // end try/catch } // end try/catch/finally return responseMessage.toString(); }