001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 *     http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014package org.gbif.ws.security;
015
016import javax.annotation.Nullable;
017
018public class RequestDataToSign {
019
020  // required
021  private String method;
022
023  // required
024  private String url;
025
026  // optional (POST or PUT only)
027  @Nullable private String contentType;
028
029  // optional (POST or PUT only)
030  @Nullable private String contentTypeMd5;
031
032  // required
033  private String user;
034
035  public String getMethod() {
036    return method;
037  }
038
039  public void setMethod(String method) {
040    this.method = method;
041  }
042
043  public String getUrl() {
044    return url;
045  }
046
047  public void setUrl(String url) {
048    this.url = url;
049  }
050
051  public String getContentType() {
052    return contentType;
053  }
054
055  public void setContentType(String contentType) {
056    this.contentType = contentType;
057  }
058
059  public String getContentTypeMd5() {
060    return contentTypeMd5;
061  }
062
063  public void setContentTypeMd5(String contentTypeMd5) {
064    this.contentTypeMd5 = contentTypeMd5;
065  }
066
067  public String getUser() {
068    return user;
069  }
070
071  public void setUser(String user) {
072    this.user = user;
073  }
074
075  /**
076   * Concatenates the information to be encrypted from a request into a single String. When the
077   * server receives an authenticated request, it compares the computed request signature with the
078   * signature provided in the request in StringToSign. For that reason this string may only contain
079   * information also available in the exact same form to the server.
080   *
081   * @return unique string for a request
082   * @see <a href="http://docs.amazonwebservices.com/AmazonS3/latest/dev/RESTAuthentication.html">AWS
083   * Docs</a>
084   */
085  public String stringToSign() {
086    StringBuilder sb = new StringBuilder();
087
088    sb.append(method);
089    sb.append('\n');
090    sb.append(url);
091    if (contentType != null) {
092      sb.append('\n');
093      sb.append(contentType.toLowerCase());
094    }
095    if (contentTypeMd5 != null) {
096      sb.append('\n');
097      sb.append(contentTypeMd5);
098    }
099    sb.append('\n');
100    sb.append(user);
101
102    return sb.toString();
103  }
104}