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));
      }
    }
  }
  // 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 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;
 }
Example #4
0
  protected void createSystemSQLPatterns() throws PersistenceException {
    ERepositoryObjectType sqlpatternsType = ERepositoryObjectType.SQLPATTERNS;
    if (sqlpatternsType == null) {
      return; // don't load, nothing to do
    }
    ILibrariesService service = null;
    if (!GlobalServiceRegister.getDefault().isServiceRegistered(ILibrariesService.class)) {
      return;
    }
    service =
        (ILibrariesService) GlobalServiceRegister.getDefault().getService(ILibrariesService.class);
    Project project = getRepositoryContext().getProject();
    FolderHelper folderHelper = getFolderHelper(project.getEmfProject());
    // will automatically set the children folders
    // FolderItem folderItem = folderHelper.getFolder("sqlPatterns/system");
    // if (folderItem == null) {
    //            folderItem = folderHelper.createFolder("sqlPatterns/system"); //$NON-NLS-1$
    // }

    List<URL> routines = service.getSystemSQLPatterns();

    List<IRepositoryViewObject> repositoryObjects = getAll(project, sqlpatternsType, false, false);

    for (URL url : routines) {
      String[] fragments = url.toString().split("/"); // $NON-NLS-1$
      String label = fragments[fragments.length - 1];
      String[] tmp = label.split("\\."); // $NON-NLS-1$

      Path relativePath = new Path(url.getFile());

      // for instance: categoryName is Teradata; fileName is
      // Loadfile.sqlpattern
      String fileName = relativePath.segment(relativePath.segmentCount() - 1);
      String categoryName = relativePath.segment(relativePath.segmentCount() - 2);

      tmp = fileName.split("\\."); // $NON-NLS-1$

      String sqlPatternLabel = tmp[0];

      SQLPatternItem existingItem = null;
      for (IRepositoryViewObject object : repositoryObjects) {
        if (object.getLabel().equals(sqlPatternLabel)
            && object.getProperty().getItem() instanceof SQLPatternItem
            && ((SQLPatternItem) object.getProperty().getItem())
                .getEltName()
                .equals(categoryName)) {
          existingItem = (SQLPatternItem) object.getProperty().getItem();
          break;
        }
      }
      // check the folder for categoryName,system,UserDefined
      // set the item's relative path in the repository view
      IPath categoryPath = new Path(categoryName);
      IPath systemPath = categoryPath.append(RepositoryConstants.SYSTEM_DIRECTORY);
      IPath userPath = categoryPath.append(RepositoryConstants.USER_DEFINED);

      IPath parentPath = new Path(ERepositoryObjectType.getFolderName(sqlpatternsType));
      if (folderHelper.getFolder(parentPath.append(categoryPath)) == null) {
        createFolder(
            getRepositoryContext().getProject(),
            sqlpatternsType,
            new Path(""),
            categoryPath //$NON-NLS-1$
                .lastSegment());
      }
      FolderItem systemFolder = folderHelper.getFolder(parentPath.append(systemPath));
      if (systemFolder == null) {
        Folder folder =
            createFolder(
                getRepositoryContext().getProject(),
                sqlpatternsType,
                categoryPath,
                systemPath.lastSegment());
        ((FolderItem) folder.getProperty().getItem()).setType(FolderType.FOLDER_LITERAL);
      }
      if (folderHelper.getFolder(parentPath.append(userPath)) == null) {
        Folder folder =
            createFolder(
                getRepositoryContext().getProject(),
                sqlpatternsType,
                categoryPath,
                userPath.lastSegment());
        ((FolderItem) folder.getProperty().getItem()).setType(FolderType.FOLDER_LITERAL);
      }
      //
      if (existingItem == null) {
        createSQLPattern(url, sqlPatternLabel, categoryName);
      } else {
        updateSQLPattern(url, existingItem);
        existingItem.setParent(systemFolder);
      }
    }
  }
  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));
  }