EMMA Coverage Report (generated Mon Oct 17 05:41:20 EDT 2011)
[all classes][org.jclouds.gogrid.filters]

COVERAGE SUMMARY FOR SOURCE FILE [SharedKeyLiteAuthentication.java]

nameclass, %method, %block, %line, %
SharedKeyLiteAuthentication.java100% (1/1)100% (4/4)95%  (111/117)91%  (21/23)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class SharedKeyLiteAuthentication100% (1/1)100% (4/4)95%  (111/117)91%  (21/23)
getMd5For (String): String 100% (1/1)40%  (4/10)33%  (1/3)
SharedKeyLiteAuthentication (String, String, Long, HttpUtils): void 100% (1/1)100% (18/18)100% (7/7)
createStringToSign (): String 100% (1/1)100% (20/20)100% (1/1)
filter (HttpRequest): HttpRequest 100% (1/1)100% (69/69)100% (12/12)

1/**
2 * Licensed to jclouds, Inc. (jclouds) under one or more
3 * contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  jclouds licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied.  See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.jclouds.gogrid.filters;
20 
21import static java.lang.String.format;
22import static org.jclouds.Constants.PROPERTY_CREDENTIAL;
23import static org.jclouds.Constants.PROPERTY_IDENTITY;
24 
25import java.net.URI;
26 
27import javax.annotation.Resource;
28import javax.inject.Inject;
29import javax.inject.Named;
30 
31import org.jclouds.Constants;
32import org.jclouds.crypto.CryptoStreams;
33import org.jclouds.date.TimeStamp;
34import org.jclouds.http.HttpRequest;
35import org.jclouds.http.HttpRequestFilter;
36import org.jclouds.http.HttpUtils;
37import org.jclouds.http.utils.ModifyRequest;
38import org.jclouds.io.InputSuppliers;
39import org.jclouds.logging.Logger;
40 
41import com.google.common.collect.ImmutableSet;
42import com.google.common.collect.Multimap;
43 
44/**
45 * @author Oleksiy Yarmula
46 */
47public class SharedKeyLiteAuthentication implements HttpRequestFilter {
48 
49   private final String apiKey;
50   private final String secret;
51   private final Long timeStamp;
52   private final HttpUtils utils;
53 
54   @Resource
55   @Named(Constants.LOGGER_SIGNATURE)
56   Logger signatureLog = Logger.NULL;
57 
58   @Inject
59   public SharedKeyLiteAuthentication(@Named(PROPERTY_IDENTITY) String apiKey,
60            @Named(PROPERTY_CREDENTIAL) String secret, @TimeStamp Long timeStamp, HttpUtils utils) {
61      this.apiKey = apiKey;
62      this.secret = secret;
63      this.timeStamp = timeStamp;
64      this.utils = utils;
65   }
66 
67   @Override
68   public HttpRequest filter(HttpRequest request) {
69 
70      String toSign = createStringToSign();
71      String signatureMd5 = getMd5For(toSign);
72 
73      String query = request.getEndpoint().getQuery();
74      Multimap<String, String> decodedParams = ModifyRequest.parseQueryToMap(query);
75 
76      decodedParams.replaceValues("sig", ImmutableSet.of(signatureMd5));
77      decodedParams.replaceValues("api_key", ImmutableSet.of(apiKey));
78 
79      String updatedQuery = ModifyRequest.makeQueryLine(decodedParams, null);
80      String requestBasePart = request.getEndpoint().toASCIIString();
81      String updatedEndpoint = requestBasePart.substring(0, requestBasePart.indexOf("?") + 1) + updatedQuery;
82      request = request.toBuilder().endpoint(URI.create(updatedEndpoint)).build();
83      utils.logRequest(signatureLog, request, "<<");
84      return request;
85   }
86 
87   private String createStringToSign() {
88      return format("%s%s%s", apiKey, secret, timeStamp);
89   }
90 
91   private String getMd5For(String stringToHash) {
92      try {
93         return CryptoStreams.md5Hex(InputSuppliers.of(stringToHash));
94      } catch (Exception e) {
95         throw new RuntimeException(e);
96      }
97   }
98 
99}

[all classes][org.jclouds.gogrid.filters]
EMMA 2.0.5312 (C) Vladimir Roubtsov