View Javadoc

1   package com.atlassian.sal.core.net.auth;
2   
3   import java.io.UnsupportedEncodingException;
4   import java.net.URLEncoder;
5   
6   import org.apache.commons.httpclient.HttpClient;
7   import org.apache.commons.httpclient.HttpMethod;
8   
9   public class SeraphAuthenticator implements HttpClientAuthenticator
10  {
11      private final String username;
12      private final String password;
13  
14      public SeraphAuthenticator(String username, String password)
15      {
16          this.username = username;
17          this.password = password;
18      }
19  
20      /**
21       * @param httpClient
22       * @param method
23       */
24      public void process(HttpClient httpClient, HttpMethod method)
25      {
26          String queryString = method.getQueryString();
27          if (queryString != null && queryString.contains("os_username"))
28          {
29              // It looks like someone has already set the username manually...
30              return;
31          }
32          if (queryString == null)
33          {
34              queryString = "";
35          }
36          else
37          {
38              queryString += "&";
39          }
40  
41          queryString += "os_username=" + urlEncode(username) + "&os_password=" + urlEncode(password);
42  
43          method.setQueryString(queryString);
44      }
45  
46      private static String urlEncode(String str)
47      {
48          try
49          {
50              return URLEncoder.encode(str, "UTF-8");
51          }
52          catch (UnsupportedEncodingException e)
53          {
54              // UTF-8 is standard, this should never happen
55              throw new RuntimeException("Funny JRE you have here, it doesn't support UTF-8");
56          }
57      }
58  }