private boolean handlePost(HttpServletRequest request, HttpServletResponse response, String path) throws IOException, JSONException, ServletException, URISyntaxException, CoreException { Path p = new Path(path); if (p.segment(0).equals("file")) { // $NON-NLS-1$ // handle adding new remote // expected path: /git/remote/file/{path} return addRemote(request, response, path); } else { JSONObject requestObject = OrionServlet.readJSONRequest(request); boolean fetch = Boolean.parseBoolean(requestObject.optString(GitConstants.KEY_FETCH, null)); String srcRef = requestObject.optString(GitConstants.KEY_PUSH_SRC_REF, null); boolean tags = requestObject.optBoolean(GitConstants.KEY_PUSH_TAGS, false); boolean force = requestObject.optBoolean(GitConstants.KEY_FORCE, false); // prepare creds GitCredentialsProvider cp = GitUtils.createGitCredentialsProvider(requestObject); // if all went well, continue with fetch or push if (fetch) { return fetch(request, response, cp, path, force); } else if (srcRef != null) { return push(request, response, path, cp, srcRef, tags, force); } else { return statusHandler.handleRequest( request, response, new ServerStatus( IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, "Only Fetch:true is currently supported.", null)); } } }
private boolean push( HttpServletRequest request, HttpServletResponse response, String path, GitCredentialsProvider cp, String srcRef, boolean tags, boolean force) throws ServletException, CoreException, IOException, JSONException, URISyntaxException { Path p = new Path(path); // FIXME: what if a remote or branch is named "file"? if (p.segment(2).equals("file")) { // $NON-NLS-1$ // /git/remote/{remote}/{branch}/file/{path} PushJob job = new PushJob(TaskJobHandler.getUserId(request), cp, p, srcRef, tags, force); return TaskJobHandler.handleTaskJob(request, response, job, statusHandler); } return false; }
// remove remote private boolean handleDelete( HttpServletRequest request, HttpServletResponse response, String path) throws CoreException, IOException, URISyntaxException, JSONException, ServletException { Path p = new Path(path); if (p.segment(1).equals("file")) { // $NON-NLS-1$ // expected path: /gitapi/remote/{remote}/file/{path} String remoteName = p.segment(0); File gitDir = GitUtils.getGitDir(p.removeFirstSegments(1)); Repository db = new FileRepository(gitDir); StoredConfig config = db.getConfig(); config.unsetSection(ConfigConstants.CONFIG_REMOTE_SECTION, remoteName); config.save(); // TODO: handle result return true; } return false; }
private boolean handleGet(HttpServletRequest request, HttpServletResponse response, String path) throws IOException, JSONException, ServletException, URISyntaxException, CoreException { Path p = new Path(path); // FIXME: what if a remote or branch is named "file"? if (p.segment(0).equals("file")) { // $NON-NLS-1$ // /git/remote/file/{path} File gitDir = GitUtils.getGitDir(p); Repository db = new FileRepository(gitDir); Set<String> configNames = db.getConfig().getSubsections(ConfigConstants.CONFIG_REMOTE_SECTION); JSONObject result = new JSONObject(); JSONArray children = new JSONArray(); URI cloneLocation = BaseToCloneConverter.getCloneLocation(getURI(request), BaseToCloneConverter.REMOTE_LIST); for (String configName : configNames) { Remote remote = new Remote(cloneLocation, db, configName); children.put(remote.toJSON(false, null)); } result.put(ProtocolConstants.KEY_CHILDREN, children); result.put(ProtocolConstants.KEY_TYPE, Remote.TYPE); OrionServlet.writeJSONResponse(request, response, result); return true; } else if (p.segment(1).equals("file")) { // $NON-NLS-1$ // /git/remote/{remote}/file/{path} RemoteDetailsJob job; String commits = request.getParameter(GitConstants.KEY_TAG_COMMITS); int commitsNumber = commits == null ? 0 : Integer.parseInt(commits); String page = request.getParameter("page"); if (page != null) { int pageNo = Integer.parseInt(page); int pageSize = request.getParameter("pageSize") == null ? PAGE_SIZE : Integer.parseInt(request.getParameter("pageSize")); job = new RemoteDetailsJob( TaskJobHandler.getUserId(request), p.segment(0), p.removeFirstSegments(1), BaseToCloneConverter.getCloneLocation(getURI(request), BaseToCloneConverter.REMOTE), commitsNumber, pageNo, pageSize, request.getRequestURI()); } else { job = new RemoteDetailsJob( TaskJobHandler.getUserId(request), p.segment(0), p.removeFirstSegments(1), BaseToCloneConverter.getCloneLocation(getURI(request), BaseToCloneConverter.REMOTE), commitsNumber); } return TaskJobHandler.handleTaskJob(request, response, job, statusHandler); } else if (p.segment(2).equals("file")) { // $NON-NLS-1$ // /git/remote/{remote}/{branch}/file/{path} File gitDir = GitUtils.getGitDir(p.removeFirstSegments(2)); Repository db = new FileRepository(gitDir); URI cloneLocation = BaseToCloneConverter.getCloneLocation( getURI(request), BaseToCloneConverter.REMOTE_BRANCH); Remote remote = new Remote(cloneLocation, db, p.segment(0)); RemoteBranch remoteBranch = new RemoteBranch(cloneLocation, db, remote, p.segment(1)); JSONObject result = remoteBranch.toJSON(); if (result != null) { OrionServlet.writeJSONResponse(request, response, result); return true; } JSONObject errorData = new JSONObject(); errorData.put(GitConstants.KEY_CLONE, cloneLocation); return statusHandler.handleRequest( request, response, new ServerStatus( new Status( IStatus.ERROR, GitActivator.PI_GIT, "No remote branch found: " + p.uptoSegment(2).removeTrailingSeparator()), HttpServletResponse.SC_NOT_FOUND, errorData)); } return statusHandler.handleRequest( request, response, new ServerStatus( IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, "Bad request, \"/git/remote/{remote}/{branch}/file/{path}\" expected", null)); }