ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 1 | package com.google.net.stubby.http; |
| 2 | |
| 3 | import com.google.common.io.ByteBuffers; |
| 4 | import com.google.net.stubby.AbstractRequest; |
lryan | e4bd1c7 | 2014-09-08 14:03:35 -0700 | [diff] [blame^] | 5 | import com.google.net.stubby.Metadata; |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 6 | import com.google.net.stubby.Operation; |
| 7 | import com.google.net.stubby.Response; |
| 8 | import com.google.net.stubby.Session; |
| 9 | import com.google.net.stubby.Status; |
| 10 | import com.google.net.stubby.transport.Framer; |
| 11 | import com.google.net.stubby.transport.MessageFramer; |
| 12 | import com.google.net.stubby.transport.Transport; |
| 13 | |
| 14 | import java.io.DataOutputStream; |
| 15 | import java.io.IOException; |
| 16 | import java.io.InputStream; |
| 17 | import java.net.HttpURLConnection; |
| 18 | import java.net.URI; |
| 19 | import java.nio.ByteBuffer; |
| 20 | |
| 21 | /** |
| 22 | * Implementation of {@link Session} using {@link HttpURLConnection} for clients. Services |
| 23 | * are dispatched relative to a base URI. |
| 24 | */ |
| 25 | public class UrlConnectionClientSession implements Session { |
| 26 | |
| 27 | private final URI base; |
| 28 | |
| 29 | public UrlConnectionClientSession(URI base) { |
| 30 | this.base = base; |
| 31 | } |
| 32 | |
| 33 | @Override |
lryan | e4bd1c7 | 2014-09-08 14:03:35 -0700 | [diff] [blame^] | 34 | public Request startRequest(String operationName, Metadata.Headers headers, |
lryan | 3465040 | 2014-07-14 13:39:39 -0700 | [diff] [blame] | 35 | Response.ResponseBuilder responseBuilder) { |
lryan | e4bd1c7 | 2014-09-08 14:03:35 -0700 | [diff] [blame^] | 36 | return new Request(base.resolve(operationName), headers, |
| 37 | responseBuilder.build()); |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | private class Request extends AbstractRequest implements Framer.Sink { |
| 41 | |
| 42 | private final HttpURLConnection connection; |
| 43 | private final DataOutputStream outputStream; |
| 44 | private final MessageFramer framer; |
| 45 | |
lryan | e4bd1c7 | 2014-09-08 14:03:35 -0700 | [diff] [blame^] | 46 | private Request(URI uri, Metadata.Headers headers, Response response) { |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 47 | super(response); |
| 48 | try { |
| 49 | connection = (HttpURLConnection) uri.toURL().openConnection(); |
| 50 | connection.setDoOutput(true); |
| 51 | connection.setDoInput(true); |
| 52 | connection.setRequestMethod("POST"); |
| 53 | connection.setRequestProperty("Content-Type", "application/protorpc"); |
lryan | e4bd1c7 | 2014-09-08 14:03:35 -0700 | [diff] [blame^] | 54 | String[] serialized = headers.serializeAscii(); |
| 55 | for (int i = 0; i < serialized.length; i++) { |
| 56 | connection.setRequestProperty( |
| 57 | serialized[i], |
| 58 | serialized[++i]); |
lryan | 3465040 | 2014-07-14 13:39:39 -0700 | [diff] [blame] | 59 | } |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 60 | outputStream = new DataOutputStream(connection.getOutputStream()); |
| 61 | } catch (IOException t) { |
| 62 | throw new RuntimeException(t); |
| 63 | } |
| 64 | // No compression when framing over HTTP for the moment |
| 65 | framer = new MessageFramer(4096); |
| 66 | framer.setAllowCompression(false); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public Operation addContext(String type, InputStream message, Phase nextPhase) { |
| 71 | super.addContext(type, message, nextPhase); |
| 72 | framer.writeContext(type, message, getPhase() == Phase.CLOSED, this); |
| 73 | return this; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public Operation addPayload(InputStream payload, Phase nextPhase) { |
| 78 | super.addPayload(payload, nextPhase); |
| 79 | framer.writePayload(payload, getPhase() == Phase.CLOSED, this); |
| 80 | return this; |
| 81 | } |
| 82 | |
| 83 | @Override |
lryan | 2ce8446 | 2014-06-02 14:43:36 -0700 | [diff] [blame] | 84 | public Operation close(Status status) { |
| 85 | // TODO(user): This is broken but necessary to get test passing with the introduction |
| 86 | // of Channel as now for most calls the close() call is decoupled from the last call to |
| 87 | // addPayload. The real fix is to remove 'nextPhase' from the Operation interface and |
| 88 | // clean up Framer. For a follow up CL. |
| 89 | boolean alreadyClosed = getPhase() == Phase.CLOSED; |
| 90 | super.close(status); |
| 91 | if (!alreadyClosed) { |
| 92 | framer.writeStatus(status, true, this); |
| 93 | } |
| 94 | return this; |
| 95 | } |
| 96 | |
| 97 | @Override |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 98 | public void deliverFrame(ByteBuffer frame, boolean endOfMessage) { |
| 99 | boolean closed = getPhase() == Phase.CLOSED; |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 100 | try { |
| 101 | ByteBuffers.asByteSource(frame).copyTo(outputStream); |
| 102 | if (closed && endOfMessage) { |
| 103 | connection.getOutputStream().close(); |
| 104 | // The request has completed so now process the response. Must do this in the same |
| 105 | // thread as URLConnection has threading issues. |
| 106 | new HttpStreamDeframer().deframe(connection.getInputStream(), getResponse()); |
| 107 | connection.getInputStream().close(); |
| 108 | connection.disconnect(); |
| 109 | } |
| 110 | } catch (IOException ioe) { |
| 111 | close(new Status(Transport.Code.INTERNAL, ioe)); |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | } |
| 115 | } |