1   package com.atlassian.plugins.rest.helloworld;
2   
3   import com.atlassian.plugins.rest.common.security.AnonymousAllowed;
4   import com.atlassian.plugins.rest.common.security.AuthenticationContext;
5   
6   import javax.ws.rs.GET;
7   import javax.ws.rs.Path;
8   import javax.ws.rs.Produces;
9   import javax.ws.rs.core.Context;
10  import java.security.Principal;
11  
12  @Path("/helloworld")
13  public class HelloWorld
14  {
15      @GET
16      @Produces("text/plain")
17      @Path("/authenticated")
18      public String getAuthenticatedMessage()
19      {
20          return "Hello Authenticated World";
21      }
22  
23      @GET
24      @Produces("text/plain")
25      @Path("/anonymous")
26      @AnonymousAllowed
27      public String getAnonymousMessage()
28      {
29          return "Hello Anonymous World";
30      }
31  
32      @GET
33      @Produces("text/plain")
34      @Path("/admin")
35      @AnonymousAllowed
36      public String getMessageForAdmin(@Context AuthenticationContext authenticationContext)
37      {
38          checkIsUser(authenticationContext, "admin");
39          return "Hello " + authenticationContext.getPrincipal();
40      }
41  
42      private void checkIsUser(AuthenticationContext context, String userName)
43      {
44          final Principal principal = context.getPrincipal();
45          if (principal == null || !principal.getName().equals(userName))
46          {
47              throw new SecurityException("You're not '" + userName + "' I know who you really are'" + principal + "', you can't access this information");
48          }
49      }
50  }