View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    *     http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  package org.gbif.ws.security;
15  
16  import javax.annotation.Nullable;
17  
18  public class RequestDataToSign {
19  
20    // required
21    private String method;
22  
23    // required
24    private String url;
25  
26    // optional (POST or PUT only)
27    @Nullable private String contentType;
28  
29    // optional (POST or PUT only)
30    @Nullable private String contentTypeMd5;
31  
32    // required
33    private String user;
34  
35    public String getMethod() {
36      return method;
37    }
38  
39    public void setMethod(String method) {
40      this.method = method;
41    }
42  
43    public String getUrl() {
44      return url;
45    }
46  
47    public void setUrl(String url) {
48      this.url = url;
49    }
50  
51    public String getContentType() {
52      return contentType;
53    }
54  
55    public void setContentType(String contentType) {
56      this.contentType = contentType;
57    }
58  
59    public String getContentTypeMd5() {
60      return contentTypeMd5;
61    }
62  
63    public void setContentTypeMd5(String contentTypeMd5) {
64      this.contentTypeMd5 = contentTypeMd5;
65    }
66  
67    public String getUser() {
68      return user;
69    }
70  
71    public void setUser(String user) {
72      this.user = user;
73    }
74  
75    /**
76     * Concatenates the information to be encrypted from a request into a single String. When the
77     * server receives an authenticated request, it compares the computed request signature with the
78     * signature provided in the request in StringToSign. For that reason this string may only contain
79     * information also available in the exact same form to the server.
80     *
81     * @return unique string for a request
82     * @see <a href="http://docs.amazonwebservices.com/AmazonS3/latest/dev/RESTAuthentication.html">AWS
83     * Docs</a>
84     */
85    public String stringToSign() {
86      StringBuilder sb = new StringBuilder();
87  
88      sb.append(method);
89      sb.append('\n');
90      sb.append(url);
91      if (contentType != null) {
92        sb.append('\n');
93        sb.append(contentType.toLowerCase());
94      }
95      if (contentTypeMd5 != null) {
96        sb.append('\n');
97        sb.append(contentTypeMd5);
98      }
99      sb.append('\n');
100     sb.append(user);
101 
102     return sb.toString();
103   }
104 }