org.gitlab.api.GitlabAPIException Java Examples

The following examples show how to use org.gitlab.api.GitlabAPIException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GitLabMapping.java    From git-as-svn with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void ready(@NotNull SharedContext context) throws IOException {
  final GitlabAPI api = gitLabContext.connect();

  // Web hook for repository list update.
  final WebServer webServer = context.sure(WebServer.class);
  final URL hookUrl = webServer.toUrl(gitLabContext.getHookPath());
  final String path = hookUrl.getPath();
  webServer.addServlet(StringUtils.isEmptyOrNull(path) ? "/" : path, new GitLabHookServlet());

  try {
    if (!isHookInstalled(api, hookUrl.toString())) {
      api.addSystemHook(hookUrl.toString());
    }
  } catch (GitlabAPIException e) {
    if (e.getResponseCode() == HttpURLConnection.HTTP_FORBIDDEN) {
      log.warn("Unable to install gitlab hook {}: {}", hookUrl, e.getMessage());
    } else {
      throw e;
    }
  }

}
 
Example #2
Source File: GitLabContext.java    From git-as-svn with GNU General Public License v2.0 6 votes vote down vote up
@NotNull
public static GitLabToken obtainAccessToken(@NotNull String gitlabUrl, @NotNull String username, @NotNull String password, boolean sudoScope) throws IOException {
  try {
    final OAuthGetAccessToken tokenServerUrl = new OAuthGetAccessToken(gitlabUrl + "/oauth/token?scope=api" + (sudoScope ? "%20sudo" : ""));
    final TokenResponse oauthResponse = new PasswordTokenRequest(transport, JacksonFactory.getDefaultInstance(), tokenServerUrl, username, password).execute();
    return new GitLabToken(TokenType.ACCESS_TOKEN, oauthResponse.getAccessToken());
  } catch (TokenResponseException e) {
    if (sudoScope && e.getStatusCode() == HttpURLConnection.HTTP_UNAUTHORIZED) {
      // Fallback for pre-10.2 gitlab versions
      final GitlabSession session = GitlabAPI.connect(gitlabUrl, username, password);
      return new GitLabToken(TokenType.PRIVATE_TOKEN, session.getPrivateToken());
    } else {
      throw new GitlabAPIException(e.getMessage(), e.getStatusCode(), e);
    }
  }
}
 
Example #3
Source File: GitlabHTTPRequestor.java    From java-gitlab-api with Apache License 2.0 6 votes vote down vote up
private void handleAPIError(IOException e, HttpURLConnection connection) throws IOException {
    if (e instanceof FileNotFoundException || // pass through 404 Not Found to allow the caller to handle it intelligently
            e instanceof SocketTimeoutException ||
            e instanceof ConnectException) {
        throw e;
    }

    InputStream es = wrapStream(connection, connection.getErrorStream());
    try {
        String error = null;
        if (es != null) {
            error = IOUtils.toString(es, "UTF-8");
        }
        throw new GitlabAPIException(error, connection.getResponseCode(), e);
    } finally {
        IOUtils.closeQuietly(es);
    }
}
 
Example #4
Source File: GitlabRepositoryApiImpl.java    From bistoury with GNU General Public License v3.0 5 votes vote down vote up
private ApiResult doFile(final String projectId, final String ref, final String filepath) throws IOException {
    try {
        final GitlabAPI api = createGitlabApi();
        final GitlabProject project = api.getProject(projectId);
        final Query query = new Query().append("file_path", filepath).append("ref", ref);
        final String url = "/projects/" + project.getId() + "/repository/files" + query.toString();
        return ResultHelper.success(api.retrieve().to(url, GitlabFile.class));
    } catch (GitlabAPIException e) {
        Metrics.counter("connect_gitlab_error").inc();
        return ResultHelper.fail(-1, "连接gitlab服务器失败,请核private token", e);
    } catch (FileNotFoundException fnfe) {
        return ResultHelper.fail(-1, "文件不存在,请核对仓库地址", fnfe);
    }
}