@Override public RingHandler response(SelectionKey key, String httpVersion, int status, String statusMessage) { throw new NetException("Should not happen."); }
public SSLContext getSslContext() { try { return sslContext != null ? sslContext : SSLContext.getDefault(); } catch (NoSuchAlgorithmException e) { throw new NetException("Error initializing SSL context", e); } }
public static EventSink getSink(String name) { EventSink sink = sinks.get(name); if (sink == null) { synchronized (sinks) { try { Object[] args = new Object[defaultSinkExtraArgs.length+1]; args[0] = name; if (defaultSinkExtraArgs.length > 0) System.arraycopy(defaultSinkExtraArgs, 0, args, 1, defaultSinkExtraArgs.length); sink = (EventSink)defaultSinkImpl.getConstructors()[0].newInstance(args); sinks.put(name, sink); } catch (Exception e) { throw new NetException("Error constructing sink object", e); } } } return sink; }
@Override public boolean submit(SelectionKey key, boolean copyOnSchedule, ByteBuffer... buffers) { try { for (ByteBuffer buf : buffers) { if (buf == NetCtx.FLUSH) { os.flush(); } else if (buf == NetCtx.CLOSE) { os.close(); } else if (buf != NetCtx.NULL) { os.write(buf.array(), buf.position(), buf.limit()); buf.clear(); } } } catch (IOException e) { throw new NetException("I/O error when writing bufs to output stream", e); } return true; } }
public HttpStreamClient(HttpConfig config, String baseUrl) { this.config = config; Matcher m = RE_URL.matcher(baseUrl); if (!m.matches()) throw new NetException("Invalid URL: " + baseUrl); this.tls = "https".equalsIgnoreCase(m.group(REG_URL_PROTO)); String pstr = m.group(HttpProtocol.REG_URL_PORT); this.port = (pstr != null) ? Integer.parseInt(pstr.substring(1)) : tls ? 443 : 80; try { this.host = m.group(HttpProtocol.REG_URL_ADDR); this.addr = InetAddress.getByName(this.host); this.config.setHost(this.host); } catch (UnknownHostException e) { throw new NetException("Error resolving host: " + m.group(2)); } this.baseUri = m.group(4) != null ? m.group(4) : "/"; if (!tls) { socketFactory = SocketFactory.getDefault(); } else { socketFactory = config.getSslContext().getSocketFactory(); } }
@Override public void run() { byte[] buf = new byte[1024]; try { for (int n = is.read(buf); n >= 0; n = is.read(buf)) { decoder.submit(null, false, ByteBuffer.wrap(buf, 0, n)); if (decoder.hasFinished()) { decoder.reset(); break; } } } catch (IOException e) { throw new NetException("I/O error", e); } } }
private void connect() { try { socket = socketFactory.createSocket(addr, port); input = new HttpStreamInput(config, this, HttpDecoderState.READ_RESP_LINE, socket.getInputStream()); stream = new BufStreamOutput(socket.getOutputStream()); output = new HttpMessageHandler(config, null); evtConnects.call(); } catch (IOException e) { evtConnects.error(); throw new NetException("Cannot connect to " + addr + ":" + port, e); } }
public HttpRequest(HttpConfig config, HttpListener listener, String url, HttpMethod method, Object...args) { super(HttpProtocol.urlToAddr(url), HttpProtocol.urlToPort(url)); this.config = config; this.listener = listener; this.decoder = new HttpDecoder(config, HttpDecoderState.READ_RESP_LINE, listener); this.method = method; Matcher m = HttpProtocol.RE_URL.matcher(url); if (m.matches()) { uri = m.group(4); query = m.group(5); } else { throw new NetException("Unparsable URL: " + url); } if (uri == null) { uri = "/"; } parseArgs(args); }
@Override public HttpMessage exec(HttpMessage req) { if (socket == null || !socket.isConnected()) { connect(); } Exception e = null; for (int i = 0; i < config.getMaxRetries(); i++) { try { output.submit(new HttpEncoder(config, stream), null, req); socket.getOutputStream().flush(); input.run(); evtCalls.call(); return result; } catch (Exception e1) { e = e1; reconnect(); } } throw new NetException("Error executing HTTP call", e); }