{"action":"created","comment":{"url":"https://api.github.com/repos/caskdata/cdap/pulls/comments/25969234","id":25969234,"diff_hunk":"@@ -0,0 +1,143 @@\n+/*\n+ * Copyright © 2015 Cask Data, Inc.\n+ *\n+ * Licensed under the Apache License, Version 2.0 (the \"License\"); you may not\n+ * use this file except in compliance with the License. You may obtain a copy of\n+ * the License at\n+ *\n+ * http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\n+ * License for the specific language governing permissions and limitations under\n+ * the License.\n+ */\n+\n+package co.cask.cdap.app.stream;\n+\n+import co.cask.cdap.api.common.Bytes;\n+import co.cask.cdap.api.data.stream.StreamBatchWriter;\n+import co.cask.cdap.api.data.stream.StreamWriter;\n+import co.cask.cdap.api.stream.StreamEventData;\n+import co.cask.cdap.common.conf.Constants;\n+import co.cask.cdap.common.discovery.RandomEndpointStrategy;\n+import co.cask.cdap.proto.Id;\n+import co.cask.common.http.HttpMethod;\n+import co.cask.common.http.HttpRequest;\n+import co.cask.common.http.HttpRequests;\n+import co.cask.common.http.HttpResponse;\n+import com.google.common.collect.ImmutableMap;\n+import com.google.common.collect.Maps;\n+import com.google.common.net.HttpHeaders;\n+import io.netty.handler.codec.http.HttpResponseStatus;\n+import org.apache.twill.discovery.Discoverable;\n+import org.apache.twill.discovery.DiscoveryServiceClient;\n+import org.apache.twill.discovery.ServiceDiscovered;\n+import org.slf4j.Logger;\n+import org.slf4j.LoggerFactory;\n+\n+import java.io.File;\n+import java.io.IOException;\n+import java.net.HttpURLConnection;\n+import java.net.InetSocketAddress;\n+import java.net.URL;\n+import java.nio.ByteBuffer;\n+import java.util.Map;\n+import java.util.concurrent.TimeUnit;\n+\n+/**\n+ * Default implementation of {@link StreamWriter}\n+ */\n+public class DefaultStreamWriter implements StreamWriter {\n+\n+  private static final Logger LOG = LoggerFactory.getLogger(DefaultStreamWriter.class);\n+\n+  private final String namespaceId;\n+  private final DiscoveryServiceClient discoveryServiceClient;\n+\n+  public DefaultStreamWriter(String namespaceId, DiscoveryServiceClient discoveryServiceClient) {\n+    this.namespaceId = namespaceId;\n+    this.discoveryServiceClient = discoveryServiceClient;\n+  }\n+\n+  private URL getStreamURL(String stream) throws IOException {\n+    return getStreamURL(stream, false);\n+  }\n+\n+  private URL getStreamURL(String stream, boolean batch) throws IOException {\n+    ServiceDiscovered discovered = discoveryServiceClient.discover(Constants.Service.STREAMS);\n+    Discoverable discoverable = new RandomEndpointStrategy(discovered).pick(1, TimeUnit.SECONDS);\n+    if (discoverable == null) {\n+      throw new IOException(\"Stream Service Endpoint not found\");\n+    }\n+\n+    InetSocketAddress address = discoverable.getSocketAddress();\n+    String path = String.format(\"http://%s:%d%s/namespaces/%s/streams/%s\", address.getHostName(), address.getPort(),\n+                                Constants.Gateway.API_VERSION_3, namespaceId, stream);\n+    if (batch) {\n+      path = String.format(\"%s/batch\", path);\n+    }\n+    return new URL(path);\n+  }\n+\n+  private void writeToStream(Id.Stream stream, HttpRequest request) throws IOException {\n+    HttpResponse response = HttpRequests.execute(request);\n+    int responseCode = response.getResponseCode();\n+    if (responseCode == HttpResponseStatus.NOT_FOUND.code()) {\n+      throw new IOException(String.format(\"Stream %s not found\", stream));\n+    }\n+\n+    if (responseCode < 200 || responseCode >= 300) {\n+      throw new IOException(String.format(\"Writing to Stream %s did not succeed. Stream Service ResponseCode : %d\",\n+                                          stream, responseCode));\n+    }\n+  }\n+\n+  private void write(String stream, ByteBuffer data, Map<String, String> headers) throws IOException {\n+    URL streamURL = getStreamURL(stream);\n+    HttpRequest request = HttpRequest.post(streamURL).withBody(data).addHeaders(headers).build();\n+    writeToStream(Id.Stream.from(namespaceId, stream), request);\n+  }\n+\n+  @Override\n+  public void write(String stream, String data) throws IOException {\n+    write(stream, data, ImmutableMap.<String, String>of());\n+  }\n+\n+  @Override\n+  public void write(String stream, String data, Map<String, String> headers) throws IOException {\n+    write(stream, ByteBuffer.wrap(Bytes.toBytes(data)), headers);","path":"cdap-app-fabric/src/main/java/co/cask/cdap/app/stream/DefaultStreamWriter.java","position":110,"original_position":110,"commit_id":"2091d2cfa0123ff9484521b36d7028042b3b1810","original_commit_id":"2091d2cfa0123ff9484521b36d7028042b3b1810","user":{"login":"chtyim","id":885032,"avatar_url":"https://avatars.githubusercontent.com/u/885032?v=3","gravatar_id":"","url":"https://api.github.com/users/chtyim","html_url":"https://github.com/chtyim","followers_url":"https://api.github.com/users/chtyim/followers","following_url":"https://api.github.com/users/chtyim/following{/other_user}","gists_url":"https://api.github.com/users/chtyim/gists{/gist_id}","starred_url":"https://api.github.com/users/chtyim/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chtyim/subscriptions","organizations_url":"https://api.github.com/users/chtyim/orgs","repos_url":"https://api.github.com/users/chtyim/repos","events_url":"https://api.github.com/users/chtyim/events{/privacy}","received_events_url":"https://api.github.com/users/chtyim/received_events","type":"User","site_admin":false},"body":"Better do `Charsets.UTF_8.encode(data)`.","created_at":"2015-03-06T19:14:46Z","updated_at":"2015-03-06T19:14:46Z","html_url":"https://github.com/caskdata/cdap/pull/1448#discussion_r25969234","pull_request_url":"https://api.github.com/repos/caskdata/cdap/pulls/1448","_links":{"self":{"href":"https://api.github.com/repos/caskdata/cdap/pulls/comments/25969234"},"html":{"href":"https://github.com/caskdata/cdap/pull/1448#discussion_r25969234"},"pull_request":{"href":"https://api.github.com/repos/caskdata/cdap/pulls/1448"}}},"pull_request":{"url":"https://api.github.com/repos/caskdata/cdap/pulls/1448","id":30253248,"html_url":"https://github.com/caskdata/cdap/pull/1448","diff_url":"https://github.com/caskdata/cdap/pull/1448.diff","patch_url":"https://github.com/caskdata/cdap/pull/1448.patch","issue_url":"https://api.github.com/repos/caskdata/cdap/issues/1448","number":1448,"state":"open","locked":false,"title":"Writing to Stream through WorkerContext","user":{"login":"gokulavasan","id":558459,"avatar_url":"https://avatars.githubusercontent.com/u/558459?v=3","gravatar_id":"","url":"https://api.github.com/users/gokulavasan","html_url":"https://github.com/gokulavasan","followers_url":"https://api.github.com/users/gokulavasan/followers","following_url":"https://api.github.com/users/gokulavasan/following{/other_user}","gists_url":"https://api.github.com/users/gokulavasan/gists{/gist_id}","starred_url":"https://api.github.com/users/gokulavasan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gokulavasan/subscriptions","organizations_url":"https://api.github.com/users/gokulavasan/orgs","repos_url":"https://api.github.com/users/gokulavasan/repos","events_url":"https://api.github.com/users/gokulavasan/events{/privacy}","received_events_url":"https://api.github.com/users/gokulavasan/received_events","type":"User","site_admin":false},"body":"Allowing Workers to write to Streams through WorkerContext.\r\n\r\n* StreamContext allows Workers to write to streams - single events and in batch.\r\n* LengthBasedContentWriter that uses BufferedContentWriter and then switches to FileBasedContentWriter once batchThreshold is crossed.\r\n\r\nBamboo: http://builds.cask.co/browse/CDAP-DUT857\r\n\r\nJIRA: https://issues.cask.co/browse/CDAP-1658\r\n","created_at":"2015-03-02T01:11:34Z","updated_at":"2015-03-06T19:14:46Z","closed_at":null,"merged_at":null,"merge_commit_sha":"6f6c820dc92db33bd3c1c1f11a855c1f01a80cba","assignee":{"login":"chtyim","id":885032,"avatar_url":"https://avatars.githubusercontent.com/u/885032?v=3","gravatar_id":"","url":"https://api.github.com/users/chtyim","html_url":"https://github.com/chtyim","followers_url":"https://api.github.com/users/chtyim/followers","following_url":"https://api.github.com/users/chtyim/following{/other_user}","gists_url":"https://api.github.com/users/chtyim/gists{/gist_id}","starred_url":"https://api.github.com/users/chtyim/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chtyim/subscriptions","organizations_url":"https://api.github.com/users/chtyim/orgs","repos_url":"https://api.github.com/users/chtyim/repos","events_url":"https://api.github.com/users/chtyim/events{/privacy}","received_events_url":"https://api.github.com/users/chtyim/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/caskdata/cdap/milestones/3","html_url":"https://github.com/caskdata/cdap/milestones/2.8.0","labels_url":"https://api.github.com/repos/caskdata/cdap/milestones/3/labels","id":944584,"number":3,"title":"2.8.0","description":null,"creator":{"login":"jwang47","id":1084704,"avatar_url":"https://avatars.githubusercontent.com/u/1084704?v=3","gravatar_id":"","url":"https://api.github.com/users/jwang47","html_url":"https://github.com/jwang47","followers_url":"https://api.github.com/users/jwang47/followers","following_url":"https://api.github.com/users/jwang47/following{/other_user}","gists_url":"https://api.github.com/users/jwang47/gists{/gist_id}","starred_url":"https://api.github.com/users/jwang47/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jwang47/subscriptions","organizations_url":"https://api.github.com/users/jwang47/orgs","repos_url":"https://api.github.com/users/jwang47/repos","events_url":"https://api.github.com/users/jwang47/events{/privacy}","received_events_url":"https://api.github.com/users/jwang47/received_events","type":"User","site_admin":false},"open_issues":10,"closed_issues":136,"state":"open","created_at":"2015-01-21T01:15:14Z","updated_at":"2015-03-06T16:06:01Z","due_on":null,"closed_at":null},"commits_url":"https://api.github.com/repos/caskdata/cdap/pulls/1448/commits","review_comments_url":"https://api.github.com/repos/caskdata/cdap/pulls/1448/comments","review_comment_url":"https://api.github.com/repos/caskdata/cdap/pulls/comments{/number}","comments_url":"https://api.github.com/repos/caskdata/cdap/issues/1448/comments","statuses_url":"https://api.github.com/repos/caskdata/cdap/statuses/2091d2cfa0123ff9484521b36d7028042b3b1810","head":{"label":"caskdata:feature/write-to-stream","ref":"feature/write-to-stream","sha":"2091d2cfa0123ff9484521b36d7028042b3b1810","user":{"login":"caskdata","id":8123023,"avatar_url":"https://avatars.githubusercontent.com/u/8123023?v=3","gravatar_id":"","url":"https://api.github.com/users/caskdata","html_url":"https://github.com/caskdata","followers_url":"https://api.github.com/users/caskdata/followers","following_url":"https://api.github.com/users/caskdata/following{/other_user}","gists_url":"https://api.github.com/users/caskdata/gists{/gist_id}","starred_url":"https://api.github.com/users/caskdata/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/caskdata/subscriptions","organizations_url":"https://api.github.com/users/caskdata/orgs","repos_url":"https://api.github.com/users/caskdata/repos","events_url":"https://api.github.com/users/caskdata/events{/privacy}","received_events_url":"https://api.github.com/users/caskdata/received_events","type":"Organization","site_admin":false},"repo":{"id":22542759,"name":"cdap","full_name":"caskdata/cdap","owner":{"login":"caskdata","id":8123023,"avatar_url":"https://avatars.githubusercontent.com/u/8123023?v=3","gravatar_id":"","url":"https://api.github.com/users/caskdata","html_url":"https://github.com/caskdata","followers_url":"https://api.github.com/users/caskdata/followers","following_url":"https://api.github.com/users/caskdata/following{/other_user}","gists_url":"https://api.github.com/users/caskdata/gists{/gist_id}","starred_url":"https://api.github.com/users/caskdata/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/caskdata/subscriptions","organizations_url":"https://api.github.com/users/caskdata/orgs","repos_url":"https://api.github.com/users/caskdata/repos","events_url":"https://api.github.com/users/caskdata/events{/privacy}","received_events_url":"https://api.github.com/users/caskdata/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/caskdata/cdap","description":"Cask Data Application Platform (CDAP)","fork":false,"url":"https://api.github.com/repos/caskdata/cdap","forks_url":"https://api.github.com/repos/caskdata/cdap/forks","keys_url":"https://api.github.com/repos/caskdata/cdap/keys{/key_id}","collaborators_url":"https://api.github.com/repos/caskdata/cdap/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/caskdata/cdap/teams","hooks_url":"https://api.github.com/repos/caskdata/cdap/hooks","issue_events_url":"https://api.github.com/repos/caskdata/cdap/issues/events{/number}","events_url":"https://api.github.com/repos/caskdata/cdap/events","assignees_url":"https://api.github.com/repos/caskdata/cdap/assignees{/user}","branches_url":"https://api.github.com/repos/caskdata/cdap/branches{/branch}","tags_url":"https://api.github.com/repos/caskdata/cdap/tags","blobs_url":"https://api.github.com/repos/caskdata/cdap/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/caskdata/cdap/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/caskdata/cdap/git/refs{/sha}","trees_url":"https://api.github.com/repos/caskdata/cdap/git/trees{/sha}","statuses_url":"https://api.github.com/repos/caskdata/cdap/statuses/{sha}","languages_url":"https://api.github.com/repos/caskdata/cdap/languages","stargazers_url":"https://api.github.com/repos/caskdata/cdap/stargazers","contributors_url":"https://api.github.com/repos/caskdata/cdap/contributors","subscribers_url":"https://api.github.com/repos/caskdata/cdap/subscribers","subscription_url":"https://api.github.com/repos/caskdata/cdap/subscription","commits_url":"https://api.github.com/repos/caskdata/cdap/commits{/sha}","git_commits_url":"https://api.github.com/repos/caskdata/cdap/git/commits{/sha}","comments_url":"https://api.github.com/repos/caskdata/cdap/comments{/number}","issue_comment_url":"https://api.github.com/repos/caskdata/cdap/issues/comments{/number}","contents_url":"https://api.github.com/repos/caskdata/cdap/contents/{+path}","compare_url":"https://api.github.com/repos/caskdata/cdap/compare/{base}...{head}","merges_url":"https://api.github.com/repos/caskdata/cdap/merges","archive_url":"https://api.github.com/repos/caskdata/cdap/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/caskdata/cdap/downloads","issues_url":"https://api.github.com/repos/caskdata/cdap/issues{/number}","pulls_url":"https://api.github.com/repos/caskdata/cdap/pulls{/number}","milestones_url":"https://api.github.com/repos/caskdata/cdap/milestones{/number}","notifications_url":"https://api.github.com/repos/caskdata/cdap/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/caskdata/cdap/labels{/name}","releases_url":"https://api.github.com/repos/caskdata/cdap/releases{/id}","created_at":"2014-08-02T09:00:10Z","updated_at":"2015-03-06T16:06:02Z","pushed_at":"2015-03-06T19:01:37Z","git_url":"git://github.com/caskdata/cdap.git","ssh_url":"git@github.com:caskdata/cdap.git","clone_url":"https://github.com/caskdata/cdap.git","svn_url":"https://github.com/caskdata/cdap","homepage":"http://cdap.io","size":794092,"stargazers_count":29,"watchers_count":29,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":9,"mirror_url":null,"open_issues_count":24,"forks":9,"open_issues":24,"watchers":29,"default_branch":"develop"}},"base":{"label":"caskdata:develop","ref":"develop","sha":"22649550621e96e76b28da50c6722cc04689b024","user":{"login":"caskdata","id":8123023,"avatar_url":"https://avatars.githubusercontent.com/u/8123023?v=3","gravatar_id":"","url":"https://api.github.com/users/caskdata","html_url":"https://github.com/caskdata","followers_url":"https://api.github.com/users/caskdata/followers","following_url":"https://api.github.com/users/caskdata/following{/other_user}","gists_url":"https://api.github.com/users/caskdata/gists{/gist_id}","starred_url":"https://api.github.com/users/caskdata/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/caskdata/subscriptions","organizations_url":"https://api.github.com/users/caskdata/orgs","repos_url":"https://api.github.com/users/caskdata/repos","events_url":"https://api.github.com/users/caskdata/events{/privacy}","received_events_url":"https://api.github.com/users/caskdata/received_events","type":"Organization","site_admin":false},"repo":{"id":22542759,"name":"cdap","full_name":"caskdata/cdap","owner":{"login":"caskdata","id":8123023,"avatar_url":"https://avatars.githubusercontent.com/u/8123023?v=3","gravatar_id":"","url":"https://api.github.com/users/caskdata","html_url":"https://github.com/caskdata","followers_url":"https://api.github.com/users/caskdata/followers","following_url":"https://api.github.com/users/caskdata/following{/other_user}","gists_url":"https://api.github.com/users/caskdata/gists{/gist_id}","starred_url":"https://api.github.com/users/caskdata/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/caskdata/subscriptions","organizations_url":"https://api.github.com/users/caskdata/orgs","repos_url":"https://api.github.com/users/caskdata/repos","events_url":"https://api.github.com/users/caskdata/events{/privacy}","received_events_url":"https://api.github.com/users/caskdata/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/caskdata/cdap","description":"Cask Data Application Platform (CDAP)","fork":false,"url":"https://api.github.com/repos/caskdata/cdap","forks_url":"https://api.github.com/repos/caskdata/cdap/forks","keys_url":"https://api.github.com/repos/caskdata/cdap/keys{/key_id}","collaborators_url":"https://api.github.com/repos/caskdata/cdap/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/caskdata/cdap/teams","hooks_url":"https://api.github.com/repos/caskdata/cdap/hooks","issue_events_url":"https://api.github.com/repos/caskdata/cdap/issues/events{/number}","events_url":"https://api.github.com/repos/caskdata/cdap/events","assignees_url":"https://api.github.com/repos/caskdata/cdap/assignees{/user}","branches_url":"https://api.github.com/repos/caskdata/cdap/branches{/branch}","tags_url":"https://api.github.com/repos/caskdata/cdap/tags","blobs_url":"https://api.github.com/repos/caskdata/cdap/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/caskdata/cdap/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/caskdata/cdap/git/refs{/sha}","trees_url":"https://api.github.com/repos/caskdata/cdap/git/trees{/sha}","statuses_url":"https://api.github.com/repos/caskdata/cdap/statuses/{sha}","languages_url":"https://api.github.com/repos/caskdata/cdap/languages","stargazers_url":"https://api.github.com/repos/caskdata/cdap/stargazers","contributors_url":"https://api.github.com/repos/caskdata/cdap/contributors","subscribers_url":"https://api.github.com/repos/caskdata/cdap/subscribers","subscription_url":"https://api.github.com/repos/caskdata/cdap/subscription","commits_url":"https://api.github.com/repos/caskdata/cdap/commits{/sha}","git_commits_url":"https://api.github.com/repos/caskdata/cdap/git/commits{/sha}","comments_url":"https://api.github.com/repos/caskdata/cdap/comments{/number}","issue_comment_url":"https://api.github.com/repos/caskdata/cdap/issues/comments{/number}","contents_url":"https://api.github.com/repos/caskdata/cdap/contents/{+path}","compare_url":"https://api.github.com/repos/caskdata/cdap/compare/{base}...{head}","merges_url":"https://api.github.com/repos/caskdata/cdap/merges","archive_url":"https://api.github.com/repos/caskdata/cdap/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/caskdata/cdap/downloads","issues_url":"https://api.github.com/repos/caskdata/cdap/issues{/number}","pulls_url":"https://api.github.com/repos/caskdata/cdap/pulls{/number}","milestones_url":"https://api.github.com/repos/caskdata/cdap/milestones{/number}","notifications_url":"https://api.github.com/repos/caskdata/cdap/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/caskdata/cdap/labels{/name}","releases_url":"https://api.github.com/repos/caskdata/cdap/releases{/id}","created_at":"2014-08-02T09:00:10Z","updated_at":"2015-03-06T16:06:02Z","pushed_at":"2015-03-06T19:01:37Z","git_url":"git://github.com/caskdata/cdap.git","ssh_url":"git@github.com:caskdata/cdap.git","clone_url":"https://github.com/caskdata/cdap.git","svn_url":"https://github.com/caskdata/cdap","homepage":"http://cdap.io","size":794092,"stargazers_count":29,"watchers_count":29,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":9,"mirror_url":null,"open_issues_count":24,"forks":9,"open_issues":24,"watchers":29,"default_branch":"develop"}},"_links":{"self":{"href":"https://api.github.com/repos/caskdata/cdap/pulls/1448"},"html":{"href":"https://github.com/caskdata/cdap/pull/1448"},"issue":{"href":"https://api.github.com/repos/caskdata/cdap/issues/1448"},"comments":{"href":"https://api.github.com/repos/caskdata/cdap/issues/1448/comments"},"review_comments":{"href":"https://api.github.com/repos/caskdata/cdap/pulls/1448/comments"},"review_comment":{"href":"https://api.github.com/repos/caskdata/cdap/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/caskdata/cdap/pulls/1448/commits"},"statuses":{"href":"https://api.github.com/repos/caskdata/cdap/statuses/2091d2cfa0123ff9484521b36d7028042b3b1810"}}}}