Example usage for org.eclipse.jgit.transport.resolver ServiceNotAuthorizedException ServiceNotAuthorizedException

List of usage examples for org.eclipse.jgit.transport.resolver ServiceNotAuthorizedException ServiceNotAuthorizedException

Introduction

In this page you can find the example usage for org.eclipse.jgit.transport.resolver ServiceNotAuthorizedException ServiceNotAuthorizedException.

Prototype

public ServiceNotAuthorizedException() 

Source Link

Document

Indicates that the requested service requires authentication.

Usage

From source file:com.gitblit.git.GitblitReceivePackFactory.java

License:Apache License

@Override
public ReceivePack create(X req, Repository db)
        throws ServiceNotEnabledException, ServiceNotAuthorizedException {

    UserModel user = UserModel.ANONYMOUS;
    String repositoryName = "";
    String origin = "";
    String gitblitUrl = "";
    int timeout = 0;
    Transport transport = null;//w w w .  j  a v a 2  s .c o  m

    if (req instanceof HttpServletRequest) {
        // http/https request may or may not be authenticated
        HttpServletRequest client = (HttpServletRequest) req;
        repositoryName = client.getAttribute("gitblitRepositoryName").toString();
        origin = client.getRemoteHost();
        gitblitUrl = HttpUtils.getGitblitURL(client);

        // determine pushing user
        String username = client.getRemoteUser();
        if (!StringUtils.isEmpty(username)) {
            UserModel u = gitblit.getUserModel(username);
            if (u != null) {
                user = u;
            }
        }

        // determine the transport
        if ("http".equals(client.getScheme())) {
            transport = Transport.HTTP;
        } else if ("https".equals(client.getScheme())) {
            transport = Transport.HTTPS;
        }
    } else if (req instanceof GitDaemonClient) {
        // git daemon request is always anonymous
        GitDaemonClient client = (GitDaemonClient) req;
        repositoryName = client.getRepositoryName();
        origin = client.getRemoteAddress().getHostAddress();

        // set timeout from Git daemon
        timeout = client.getDaemon().getTimeout();

        transport = Transport.GIT;
    } else if (req instanceof SshDaemonClient) {
        // SSH request is always authenticated
        SshDaemonClient client = (SshDaemonClient) req;
        repositoryName = client.getRepositoryName();
        origin = client.getRemoteAddress().toString();
        user = client.getUser();

        transport = Transport.SSH;
    }

    if (!acceptPush(transport)) {
        throw new ServiceNotAuthorizedException();
    }

    boolean allowAnonymousPushes = settings.getBoolean(Keys.git.allowAnonymousPushes, false);
    if (!allowAnonymousPushes && UserModel.ANONYMOUS.equals(user)) {
        // prohibit anonymous pushes
        throw new ServiceNotEnabledException();
    }

    String url = settings.getString(Keys.web.canonicalUrl, null);
    if (StringUtils.isEmpty(url)) {
        url = gitblitUrl;
    }

    final RepositoryModel repository = gitblit.getRepositoryModel(repositoryName);

    // Determine which receive pack to use for pushes
    final GitblitReceivePack rp;
    if (gitblit.getTicketService().isAcceptingNewPatchsets(repository)) {
        rp = new PatchsetReceivePack(gitblit, db, repository, user);
    } else {
        rp = new GitblitReceivePack(gitblit, db, repository, user);
    }

    rp.setGitblitUrl(url);
    rp.setRefLogIdent(new PersonIdent(user.username, user.username + "@" + origin));
    rp.setTimeout(timeout);

    return rp;
}