{"action":"created","comment":{"url":"https://api.github.com/repos/codenvy/che-core/pulls/comments/48727810","id":48727810,"diff_hunk":"@@ -0,0 +1,274 @@\n+/*******************************************************************************\n+ * Copyright (c) 2012-2015 Codenvy, S.A.\n+ * All rights reserved. This program and the accompanying materials\n+ * are made available under the terms of the Eclipse Public License v1.0\n+ * which accompanies this distribution, and is available at\n+ * http://www.eclipse.org/legal/epl-v10.html\n+ *\n+ * Contributors:\n+ *   Codenvy, S.A. - initial API and implementation\n+ *******************************************************************************/\n+package org.eclipse.che.api.ssh.server;\n+\n+import com.wordnik.swagger.annotations.Api;\n+import com.wordnik.swagger.annotations.ApiOperation;\n+import com.wordnik.swagger.annotations.ApiParam;\n+import com.wordnik.swagger.annotations.ApiResponse;\n+import com.wordnik.swagger.annotations.ApiResponses;\n+\n+import org.apache.commons.fileupload.FileItem;\n+import org.eclipse.che.api.core.BadRequestException;\n+import org.eclipse.che.api.core.ConflictException;\n+import org.eclipse.che.api.core.NotFoundException;\n+import org.eclipse.che.api.core.ServerException;\n+import org.eclipse.che.api.core.rest.Service;\n+import org.eclipse.che.api.core.rest.annotations.GenerateLink;\n+import org.eclipse.che.api.core.rest.shared.dto.Link;\n+import org.eclipse.che.api.core.util.LinksHelper;\n+import org.eclipse.che.api.ssh.server.model.impl.SshPairImpl;\n+import org.eclipse.che.api.ssh.shared.Constants;\n+import org.eclipse.che.api.ssh.shared.dto.GenerateSshPairRequest;\n+import org.eclipse.che.api.ssh.shared.dto.SshPairDTO;\n+import org.eclipse.che.api.ssh.shared.model.SshPair;\n+import org.eclipse.che.commons.env.EnvironmentContext;\n+\n+import javax.annotation.security.RolesAllowed;\n+import javax.inject.Inject;\n+import javax.ws.rs.Consumes;\n+import javax.ws.rs.DELETE;\n+import javax.ws.rs.GET;\n+import javax.ws.rs.POST;\n+import javax.ws.rs.Path;\n+import javax.ws.rs.PathParam;\n+import javax.ws.rs.Produces;\n+import javax.ws.rs.core.MediaType;\n+import javax.ws.rs.core.Response;\n+import javax.ws.rs.core.UriBuilder;\n+import java.util.Arrays;\n+import java.util.Iterator;\n+import java.util.List;\n+import java.util.stream.Collectors;\n+\n+import static javax.ws.rs.core.MediaType.APPLICATION_JSON;\n+import static org.eclipse.che.api.ssh.shared.Constants.LINK_REL_GET_PAIR;\n+import static org.eclipse.che.api.ssh.shared.Constants.LINK_REL_REMOVE_PAIR;\n+import static org.eclipse.che.dto.server.DtoFactory.newDto;\n+\n+/**\n+ * Defines Ssh Rest API.\n+ *\n+ * @author Sergii Leschenko\n+ */\n+@Api(value = \"/ssh\", description = \"Ssh REST API\")\n+@Path(\"/ssh\")\n+public class SshService extends Service {\n+    private final SshManager sshManager;\n+\n+    @Inject\n+    public SshService(SshManager sshManager) {\n+        this.sshManager = sshManager;\n+    }\n+\n+    @POST\n+    @Path(\"generate\")\n+    @Consumes(APPLICATION_JSON)\n+    @Produces(APPLICATION_JSON)\n+    @RolesAllowed(\"user\")\n+    @GenerateLink(rel = Constants.LINK_REL_GENERATE_PAIR)\n+    @ApiOperation(value = \"Generate and stores ssh pair based on the request\",\n+                  notes = \"This operation can be performed only by authorized user,\" +\n+                          \"this user will be the owner of the created ssh pair\",\n+                  response = SshPairDTO.class)\n+    @ApiResponses({@ApiResponse(code = 201, message = \"The ssh pair successfully generated\"),\n+                   @ApiResponse(code = 400, message = \"Missed required parameters, parameters are not valid\"),\n+                   @ApiResponse(code = 409, message = \"Conflict error occurred during the ssh pair generation\" +\n+                                                      \"(e.g. The Ssh pair with such name and service already exists)\"),\n+                   @ApiResponse(code = 500, message = \"Internal server error occurred\")})\n+    public SshPairDTO generatePair(@ApiParam(value = \"The configuration to generate the new ssh pair\", required = true)\n+                                   GenerateSshPairRequest request) throws BadRequestException, ServerException, ConflictException {\n+        requiredNotNull(request, \"Generate ssh pair request required\");\n+        requiredNotNull(request.getService(), \"Service name required\");\n+        requiredNotNull(request.getName(), \"Name required\");\n+\n+        return injectLinks(asDto(sshManager.generatePair(getCurrentUserId(), request.getService(), request.getName())));\n+    }\n+\n+    @POST\n+    @Consumes(MediaType.MULTIPART_FORM_DATA)\n+    @RolesAllowed(\"user\")\n+    @GenerateLink(rel = Constants.LINK_REL_CREATE_PAIR)\n+    public Response createPair(Iterator<FileItem> formData) throws BadRequestException, ServerException, ConflictException {\n+        String service = null;\n+        String name = null;\n+        String privateKey = null;\n+        String publicKey = null;\n+\n+        while (formData.hasNext()) {\n+            FileItem item = formData.next();\n+            String fieldName = item.getFieldName();\n+            switch (fieldName) {\n+                case \"service\":\n+                    service = item.getString();\n+                    break;\n+                case \"name\":\n+                    name = item.getString();\n+                    break;\n+                case \"privateKey\":\n+                    privateKey = item.getString();\n+                    break;\n+                case \"publicKey\":\n+                    publicKey = item.getString();\n+                    break;\n+            }\n+        }\n+\n+        requiredNotNull(service, \"Service name required\");\n+        requiredNotNull(name, \"Name required\");\n+        if (privateKey == null && publicKey == null) {\n+            throw new BadRequestException(\"Key content was not provided.\");\n+        }\n+\n+        sshManager.createPair(getCurrentUserId(), new SshPairImpl(service, name, publicKey, privateKey));\n+\n+        // We should send 200 response code and body with empty line\n+        // through specific of html form that doesn't invoke complete submit handler\n+        return Response.ok(\"\", MediaType.TEXT_HTML).build();\n+    }\n+\n+    @POST\n+    @Consumes(APPLICATION_JSON)\n+    @RolesAllowed(\"user\")\n+    @GenerateLink(rel = Constants.LINK_REL_CREATE_PAIR)\n+    @ApiOperation(value = \"Create a new ssh pair\",\n+                  notes = \"This operation can be performed only by authorized user,\" +\n+                          \"this user will be the owner of the created ssh pair\")\n+    @ApiResponses({@ApiResponse(code = 201, message = \"The ssh pair successfully created\"),","path":"platform-api/che-core-api-ssh/src/main/java/org/eclipse/che/api/ssh/server/SshService.java","position":145,"original_position":145,"commit_id":"7d010da54a49686b1672db5f58865442b7bf8b54","original_commit_id":"7d010da54a49686b1672db5f58865442b7bf8b54","user":{"login":"sleshchenko","id":5887312,"avatar_url":"https://avatars.githubusercontent.com/u/5887312?v=3","gravatar_id":"","url":"https://api.github.com/users/sleshchenko","html_url":"https://github.com/sleshchenko","followers_url":"https://api.github.com/users/sleshchenko/followers","following_url":"https://api.github.com/users/sleshchenko/following{/other_user}","gists_url":"https://api.github.com/users/sleshchenko/gists{/gist_id}","starred_url":"https://api.github.com/users/sleshchenko/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sleshchenko/subscriptions","organizations_url":"https://api.github.com/users/sleshchenko/orgs","repos_url":"https://api.github.com/users/sleshchenko/repos","events_url":"https://api.github.com/users/sleshchenko/events{/privacy}","received_events_url":"https://api.github.com/users/sleshchenko/received_events","type":"User","site_admin":false},"body":"Thanks","created_at":"2016-01-04T12:53:41Z","updated_at":"2016-01-04T12:53:41Z","html_url":"https://github.com/codenvy/che-core/pull/582#discussion_r48727810","pull_request_url":"https://api.github.com/repos/codenvy/che-core/pulls/582","_links":{"self":{"href":"https://api.github.com/repos/codenvy/che-core/pulls/comments/48727810"},"html":{"href":"https://github.com/codenvy/che-core/pull/582#discussion_r48727810"},"pull_request":{"href":"https://api.github.com/repos/codenvy/che-core/pulls/582"}}},"pull_request":{"url":"https://api.github.com/repos/codenvy/che-core/pulls/582","id":54715838,"html_url":"https://github.com/codenvy/che-core/pull/582","diff_url":"https://github.com/codenvy/che-core/pull/582.diff","patch_url":"https://github.com/codenvy/che-core/pull/582.patch","issue_url":"https://api.github.com/repos/codenvy/che-core/issues/582","number":582,"state":"open","locked":false,"title":"IDEX-3920 Add SSH API","user":{"login":"sleshchenko","id":5887312,"avatar_url":"https://avatars.githubusercontent.com/u/5887312?v=3","gravatar_id":"","url":"https://api.github.com/users/sleshchenko","html_url":"https://github.com/sleshchenko","followers_url":"https://api.github.com/users/sleshchenko/followers","following_url":"https://api.github.com/users/sleshchenko/following{/other_user}","gists_url":"https://api.github.com/users/sleshchenko/gists{/gist_id}","starred_url":"https://api.github.com/users/sleshchenko/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sleshchenko/subscriptions","organizations_url":"https://api.github.com/users/sleshchenko/orgs","repos_url":"https://api.github.com/users/sleshchenko/repos","events_url":"https://api.github.com/users/sleshchenko/events{/privacy}","received_events_url":"https://api.github.com/users/sleshchenko/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-12-29T10:17:35Z","updated_at":"2016-01-04T12:53:41Z","closed_at":null,"merged_at":null,"merge_commit_sha":"fb2dc1e1cd4a78fcd9b91e4280fd3e8a59541149","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/codenvy/che-core/pulls/582/commits","review_comments_url":"https://api.github.com/repos/codenvy/che-core/pulls/582/comments","review_comment_url":"https://api.github.com/repos/codenvy/che-core/pulls/comments{/number}","comments_url":"https://api.github.com/repos/codenvy/che-core/issues/582/comments","statuses_url":"https://api.github.com/repos/codenvy/che-core/statuses/7d010da54a49686b1672db5f58865442b7bf8b54","head":{"label":"codenvy:IDEX-3920","ref":"IDEX-3920","sha":"7d010da54a49686b1672db5f58865442b7bf8b54","user":{"login":"codenvy","id":2736247,"avatar_url":"https://avatars.githubusercontent.com/u/2736247?v=3","gravatar_id":"","url":"https://api.github.com/users/codenvy","html_url":"https://github.com/codenvy","followers_url":"https://api.github.com/users/codenvy/followers","following_url":"https://api.github.com/users/codenvy/following{/other_user}","gists_url":"https://api.github.com/users/codenvy/gists{/gist_id}","starred_url":"https://api.github.com/users/codenvy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/codenvy/subscriptions","organizations_url":"https://api.github.com/users/codenvy/orgs","repos_url":"https://api.github.com/users/codenvy/repos","events_url":"https://api.github.com/users/codenvy/events{/privacy}","received_events_url":"https://api.github.com/users/codenvy/received_events","type":"Organization","site_admin":false},"repo":{"id":32935706,"name":"che-core","full_name":"codenvy/che-core","owner":{"login":"codenvy","id":2736247,"avatar_url":"https://avatars.githubusercontent.com/u/2736247?v=3","gravatar_id":"","url":"https://api.github.com/users/codenvy","html_url":"https://github.com/codenvy","followers_url":"https://api.github.com/users/codenvy/followers","following_url":"https://api.github.com/users/codenvy/following{/other_user}","gists_url":"https://api.github.com/users/codenvy/gists{/gist_id}","starred_url":"https://api.github.com/users/codenvy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/codenvy/subscriptions","organizations_url":"https://api.github.com/users/codenvy/orgs","repos_url":"https://api.github.com/users/codenvy/repos","events_url":"https://api.github.com/users/codenvy/events{/privacy}","received_events_url":"https://api.github.com/users/codenvy/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/codenvy/che-core","description":"Core for che","fork":false,"url":"https://api.github.com/repos/codenvy/che-core","forks_url":"https://api.github.com/repos/codenvy/che-core/forks","keys_url":"https://api.github.com/repos/codenvy/che-core/keys{/key_id}","collaborators_url":"https://api.github.com/repos/codenvy/che-core/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/codenvy/che-core/teams","hooks_url":"https://api.github.com/repos/codenvy/che-core/hooks","issue_events_url":"https://api.github.com/repos/codenvy/che-core/issues/events{/number}","events_url":"https://api.github.com/repos/codenvy/che-core/events","assignees_url":"https://api.github.com/repos/codenvy/che-core/assignees{/user}","branches_url":"https://api.github.com/repos/codenvy/che-core/branches{/branch}","tags_url":"https://api.github.com/repos/codenvy/che-core/tags","blobs_url":"https://api.github.com/repos/codenvy/che-core/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/codenvy/che-core/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/codenvy/che-core/git/refs{/sha}","trees_url":"https://api.github.com/repos/codenvy/che-core/git/trees{/sha}","statuses_url":"https://api.github.com/repos/codenvy/che-core/statuses/{sha}","languages_url":"https://api.github.com/repos/codenvy/che-core/languages","stargazers_url":"https://api.github.com/repos/codenvy/che-core/stargazers","contributors_url":"https://api.github.com/repos/codenvy/che-core/contributors","subscribers_url":"https://api.github.com/repos/codenvy/che-core/subscribers","subscription_url":"https://api.github.com/repos/codenvy/che-core/subscription","commits_url":"https://api.github.com/repos/codenvy/che-core/commits{/sha}","git_commits_url":"https://api.github.com/repos/codenvy/che-core/git/commits{/sha}","comments_url":"https://api.github.com/repos/codenvy/che-core/comments{/number}","issue_comment_url":"https://api.github.com/repos/codenvy/che-core/issues/comments{/number}","contents_url":"https://api.github.com/repos/codenvy/che-core/contents/{+path}","compare_url":"https://api.github.com/repos/codenvy/che-core/compare/{base}...{head}","merges_url":"https://api.github.com/repos/codenvy/che-core/merges","archive_url":"https://api.github.com/repos/codenvy/che-core/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/codenvy/che-core/downloads","issues_url":"https://api.github.com/repos/codenvy/che-core/issues{/number}","pulls_url":"https://api.github.com/repos/codenvy/che-core/pulls{/number}","milestones_url":"https://api.github.com/repos/codenvy/che-core/milestones{/number}","notifications_url":"https://api.github.com/repos/codenvy/che-core/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/codenvy/che-core/labels{/name}","releases_url":"https://api.github.com/repos/codenvy/che-core/releases{/id}","created_at":"2015-03-26T15:30:10Z","updated_at":"2015-12-28T01:12:02Z","pushed_at":"2016-01-04T12:34:05Z","git_url":"git://github.com/codenvy/che-core.git","ssh_url":"git@github.com:codenvy/che-core.git","clone_url":"https://github.com/codenvy/che-core.git","svn_url":"https://github.com/codenvy/che-core","homepage":null,"size":11853,"stargazers_count":20,"watchers_count":20,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":29,"mirror_url":null,"open_issues_count":9,"forks":29,"open_issues":9,"watchers":20,"default_branch":"master"}},"base":{"label":"codenvy:master","ref":"master","sha":"f61a0741881248d25524f2497edeab931a2e0b5b","user":{"login":"codenvy","id":2736247,"avatar_url":"https://avatars.githubusercontent.com/u/2736247?v=3","gravatar_id":"","url":"https://api.github.com/users/codenvy","html_url":"https://github.com/codenvy","followers_url":"https://api.github.com/users/codenvy/followers","following_url":"https://api.github.com/users/codenvy/following{/other_user}","gists_url":"https://api.github.com/users/codenvy/gists{/gist_id}","starred_url":"https://api.github.com/users/codenvy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/codenvy/subscriptions","organizations_url":"https://api.github.com/users/codenvy/orgs","repos_url":"https://api.github.com/users/codenvy/repos","events_url":"https://api.github.com/users/codenvy/events{/privacy}","received_events_url":"https://api.github.com/users/codenvy/received_events","type":"Organization","site_admin":false},"repo":{"id":32935706,"name":"che-core","full_name":"codenvy/che-core","owner":{"login":"codenvy","id":2736247,"avatar_url":"https://avatars.githubusercontent.com/u/2736247?v=3","gravatar_id":"","url":"https://api.github.com/users/codenvy","html_url":"https://github.com/codenvy","followers_url":"https://api.github.com/users/codenvy/followers","following_url":"https://api.github.com/users/codenvy/following{/other_user}","gists_url":"https://api.github.com/users/codenvy/gists{/gist_id}","starred_url":"https://api.github.com/users/codenvy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/codenvy/subscriptions","organizations_url":"https://api.github.com/users/codenvy/orgs","repos_url":"https://api.github.com/users/codenvy/repos","events_url":"https://api.github.com/users/codenvy/events{/privacy}","received_events_url":"https://api.github.com/users/codenvy/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/codenvy/che-core","description":"Core for che","fork":false,"url":"https://api.github.com/repos/codenvy/che-core","forks_url":"https://api.github.com/repos/codenvy/che-core/forks","keys_url":"https://api.github.com/repos/codenvy/che-core/keys{/key_id}","collaborators_url":"https://api.github.com/repos/codenvy/che-core/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/codenvy/che-core/teams","hooks_url":"https://api.github.com/repos/codenvy/che-core/hooks","issue_events_url":"https://api.github.com/repos/codenvy/che-core/issues/events{/number}","events_url":"https://api.github.com/repos/codenvy/che-core/events","assignees_url":"https://api.github.com/repos/codenvy/che-core/assignees{/user}","branches_url":"https://api.github.com/repos/codenvy/che-core/branches{/branch}","tags_url":"https://api.github.com/repos/codenvy/che-core/tags","blobs_url":"https://api.github.com/repos/codenvy/che-core/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/codenvy/che-core/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/codenvy/che-core/git/refs{/sha}","trees_url":"https://api.github.com/repos/codenvy/che-core/git/trees{/sha}","statuses_url":"https://api.github.com/repos/codenvy/che-core/statuses/{sha}","languages_url":"https://api.github.com/repos/codenvy/che-core/languages","stargazers_url":"https://api.github.com/repos/codenvy/che-core/stargazers","contributors_url":"https://api.github.com/repos/codenvy/che-core/contributors","subscribers_url":"https://api.github.com/repos/codenvy/che-core/subscribers","subscription_url":"https://api.github.com/repos/codenvy/che-core/subscription","commits_url":"https://api.github.com/repos/codenvy/che-core/commits{/sha}","git_commits_url":"https://api.github.com/repos/codenvy/che-core/git/commits{/sha}","comments_url":"https://api.github.com/repos/codenvy/che-core/comments{/number}","issue_comment_url":"https://api.github.com/repos/codenvy/che-core/issues/comments{/number}","contents_url":"https://api.github.com/repos/codenvy/che-core/contents/{+path}","compare_url":"https://api.github.com/repos/codenvy/che-core/compare/{base}...{head}","merges_url":"https://api.github.com/repos/codenvy/che-core/merges","archive_url":"https://api.github.com/repos/codenvy/che-core/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/codenvy/che-core/downloads","issues_url":"https://api.github.com/repos/codenvy/che-core/issues{/number}","pulls_url":"https://api.github.com/repos/codenvy/che-core/pulls{/number}","milestones_url":"https://api.github.com/repos/codenvy/che-core/milestones{/number}","notifications_url":"https://api.github.com/repos/codenvy/che-core/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/codenvy/che-core/labels{/name}","releases_url":"https://api.github.com/repos/codenvy/che-core/releases{/id}","created_at":"2015-03-26T15:30:10Z","updated_at":"2015-12-28T01:12:02Z","pushed_at":"2016-01-04T12:34:05Z","git_url":"git://github.com/codenvy/che-core.git","ssh_url":"git@github.com:codenvy/che-core.git","clone_url":"https://github.com/codenvy/che-core.git","svn_url":"https://github.com/codenvy/che-core","homepage":null,"size":11853,"stargazers_count":20,"watchers_count":20,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":29,"mirror_url":null,"open_issues_count":9,"forks":29,"open_issues":9,"watchers":20,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/codenvy/che-core/pulls/582"},"html":{"href":"https://github.com/codenvy/che-core/pull/582"},"issue":{"href":"https://api.github.com/repos/codenvy/che-core/issues/582"},"comments":{"href":"https://api.github.com/repos/codenvy/che-core/issues/582/comments"},"review_comments":{"href":"https://api.github.com/repos/codenvy/che-core/pulls/582/comments"},"review_comment":{"href":"https://api.github.com/repos/codenvy/che-core/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/codenvy/che-core/pulls/582/commits"},"statuses":{"href":"https://api.github.com/repos/codenvy/che-core/statuses/7d010da54a49686b1672db5f58865442b7bf8b54"}}}}