diff --git a/api/src/com/todoroo/andlib/service/HttpRestClient.java b/api/src/com/todoroo/andlib/service/HttpRestClient.java index c0c24df52..48b7385cf 100644 --- a/api/src/com/todoroo/andlib/service/HttpRestClient.java +++ b/api/src/com/todoroo/andlib/service/HttpRestClient.java @@ -115,23 +115,27 @@ public class HttpRestClient implements RestClient { int statusCode = response.getStatusLine().getStatusCode(); if(statusCode >= HTTP_UNAVAILABLE_START && statusCode <= HTTP_UNAVAILABLE_END) { throw new HttpUnavailableException(); - } else if(statusCode != HTTP_OK) { - throw new HttpErrorException(response.getStatusLine().getStatusCode(), - response.getStatusLine().getReasonPhrase()); } HttpEntity entity = response.getEntity(); + String body = null; if (entity != null) { InputStream contentStream = entity.getContent(); try { - return convertStreamToString(contentStream); + body = convertStreamToString(contentStream); } finally { contentStream.close(); } } - return null; + if(statusCode != HTTP_OK) { + System.out.println(body); + throw new HttpErrorException(response.getStatusLine().getStatusCode(), + response.getStatusLine().getReasonPhrase()); + } + + return body; } /** diff --git a/astrid/res/layout/web_service_text_row.xml b/astrid/res/layout/web_service_text_row.xml index 7ef479b7f..481243e8e 100644 --- a/astrid/res/layout/web_service_text_row.xml +++ b/astrid/res/layout/web_service_text_row.xml @@ -2,22 +2,18 @@ + android:layout_height="wrap_content" + android:background="#9ee5ff"> @@ -25,10 +21,11 @@ @@ -41,6 +38,6 @@ android:layout_marginTop="15dip" android:paddingLeft="7dip" android:paddingRight="7dip" - android:gravity="center" /> + android:scaleType="center" /> diff --git a/astrid/src/com/todoroo/astrid/activity/AdTestActivity.java b/astrid/src/com/todoroo/astrid/activity/AdTestActivity.java index ad747a945..0a1318062 100644 --- a/astrid/src/com/todoroo/astrid/activity/AdTestActivity.java +++ b/astrid/src/com/todoroo/astrid/activity/AdTestActivity.java @@ -4,7 +4,6 @@ import android.app.Activity; import android.os.Bundle; import android.view.ViewGroup.LayoutParams; import android.widget.FrameLayout; -import android.widget.ScrollView; import com.todoroo.astrid.data.Task; import com.todoroo.astrid.service.AstridDependencyInjector; @@ -26,10 +25,8 @@ public class AdTestActivity extends Activity { webServicesView.setLayoutParams(new FrameLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); webServicesView.setPadding(10, 10, 10, 10); - ScrollView scrollView = new ScrollView(this); - scrollView.addView(webServicesView); - setContentView(scrollView); + setContentView(webServicesView); Task task = new Task(); task.setValue(Task.TITLE, "America (The Book)"); //$NON-NLS-1$ diff --git a/astrid/src/com/todoroo/astrid/helper/AmazonRequestsHelper.java b/astrid/src/com/todoroo/astrid/helper/AmazonRequestsHelper.java new file mode 100644 index 000000000..5fdb3aeeb --- /dev/null +++ b/astrid/src/com/todoroo/astrid/helper/AmazonRequestsHelper.java @@ -0,0 +1,294 @@ +/********************************************************************************************** + * Copyright 2009 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file + * except in compliance with the License. A copy of the License is located at + * + * http://aws.amazon.com/apache2.0/ + * + * or in the "LICENSE.txt" file accompanying this file. This file is distributed on an "AS IS" + * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under the License. + * + * ******************************************************************************************** + * + * Amazon Product Advertising API + * Signed Requests Sample Code + * + * API Version: 2009-03-31 + * + */ + +package com.todoroo.astrid.helper; + +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.net.URLEncoder; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.SortedMap; +import java.util.TimeZone; +import java.util.TreeMap; + +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; + +import org.apache.commons.codec.binary.Base64; + +/** + * This class contains all the logic for signing requests + * to the Amazon Product Advertising API. + */ +@SuppressWarnings("nls") +public class AmazonRequestsHelper { + /** + * All strings are handled as UTF-8 + */ + private static final String UTF8_CHARSET = "UTF-8"; + + /** + * The HMAC algorithm required by Amazon + */ + private static final String HMAC_SHA256_ALGORITHM = "HmacSHA256"; + + /** + * This is the URI for the service, don't change unless you really know + * what you're doing. + */ + private static final String REQUEST_URI = "/onca/xml"; + + /** + * The sample uses HTTP GET to fetch the response. If you changed the sample + * to use HTTP POST instead, change the value below to POST. + */ + private static final String REQUEST_METHOD = "GET"; + + private String endpoint = null; + private String awsAccessKeyId = null; + private String awsSecretKey = null; + + private SecretKeySpec secretKeySpec = null; + private Mac mac = null; + + /** + * You must provide the three values below to initialize the helper. + * + * @param endpoint Destination for the requests. + * @param awsAccessKeyId Your AWS Access Key ID + * @param awsSecretKey Your AWS Secret Key + */ + public static AmazonRequestsHelper getInstance( + String endpoint, + String awsAccessKeyId, + String awsSecretKey + ) throws IllegalArgumentException, UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException + { + if (null == endpoint || endpoint.length() == 0) + { throw new IllegalArgumentException("endpoint is null or empty"); } + if (null == awsAccessKeyId || awsAccessKeyId.length() == 0) + { throw new IllegalArgumentException("awsAccessKeyId is null or empty"); } + if (null == awsSecretKey || awsSecretKey.length() == 0) + { throw new IllegalArgumentException("awsSecretKey is null or empty"); } + + AmazonRequestsHelper instance = new AmazonRequestsHelper(); + instance.endpoint = endpoint.toLowerCase(); + instance.awsAccessKeyId = awsAccessKeyId; + instance.awsSecretKey = awsSecretKey; + + byte[] secretyKeyBytes = instance.awsSecretKey.getBytes(UTF8_CHARSET); + instance.secretKeySpec = new SecretKeySpec(secretyKeyBytes, HMAC_SHA256_ALGORITHM); + instance.mac = Mac.getInstance(HMAC_SHA256_ALGORITHM); + instance.mac.init(instance.secretKeySpec); + + return instance; + } + + /** + * The construct is private since we'd rather use getInstance() + */ + private AmazonRequestsHelper() { + // + } + + /** + * This method signs requests in hashmap form. It returns a URL that should + * be used to fetch the response. The URL returned should not be modified in + * any way, doing so will invalidate the signature and Amazon will reject + * the request. + */ + public String sign(Map params) { + // Let's add the AWSAccessKeyId and Timestamp parameters to the request. + params.put("AWSAccessKeyId", this.awsAccessKeyId); + params.put("Timestamp", this.timestamp()); + + // The parameters need to be processed in lexicographical order, so we'll + // use a TreeMap implementation for that. + SortedMap sortedParamMap = new TreeMap(params); + + // get the canonical form the query string + String canonicalQS = this.canonicalize(sortedParamMap); + + // create the string upon which the signature is calculated + String toSign = + REQUEST_METHOD + "\n" + + this.endpoint + "\n" + + REQUEST_URI + "\n" + + canonicalQS; + + // get the signature + String hmac = this.hmac(toSign); + String sig = this.percentEncodeRfc3986(hmac); + + // construct the URL + String url = + "http://" + this.endpoint + REQUEST_URI + "?" + canonicalQS + "&Signature=" + sig; + + return url; + } + + /** + * This method signs requests in query-string form. It returns a URL that + * should be used to fetch the response. The URL returned should not be + * modified in any way, doing so will invalidate the signature and Amazon + * will reject the request. + */ + public String sign(String queryString) { + // let's break the query string into it's constituent name-value pairs + Map params = this.createParameterMap(queryString); + + // then we can sign the request as before + return this.sign(params); + } + + /** + * Compute the HMAC. + * + * @param stringToSign String to compute the HMAC over. + * @return base64-encoded hmac value. + */ + private String hmac(String stringToSign) { + String signature = null; + byte[] data; + byte[] rawHmac; + try { + data = stringToSign.getBytes(UTF8_CHARSET); + rawHmac = mac.doFinal(data); + Base64 encoder = new Base64(); + signature = new String(encoder.encode(rawHmac)); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(UTF8_CHARSET + " is unsupported!", e); + } + return signature; + } + + /** + * Generate a ISO-8601 format timestamp as required by Amazon. + * + * @return ISO-8601 format timestamp. + */ + private String timestamp() { + String timestamp = null; + Calendar cal = Calendar.getInstance(); + DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); + dfm.setTimeZone(TimeZone.getTimeZone("GMT")); + timestamp = dfm.format(cal.getTime()); + return timestamp; + } + + /** + * Canonicalize the query string as required by Amazon. + * + * @param sortedParamMap Parameter name-value pairs in lexicographical order. + * @return Canonical form of query string. + */ + private String canonicalize(SortedMap sortedParamMap) { + if (sortedParamMap.isEmpty()) { + return ""; + } + + StringBuffer buffer = new StringBuffer(); + Iterator> iter = sortedParamMap.entrySet().iterator(); + + while (iter.hasNext()) { + Map.Entry kvpair = iter.next(); + buffer.append(percentEncodeRfc3986(kvpair.getKey())); + buffer.append("="); + buffer.append(percentEncodeRfc3986(kvpair.getValue())); + if (iter.hasNext()) { + buffer.append("&"); + } + } + String cannoical = buffer.toString(); + return cannoical; + } + + /** + * Percent-encode values according the RFC 3986. The built-in Java + * URLEncoder does not encode according to the RFC, so we make the + * extra replacements. + * + * @param s decoded string + * @return encoded string per RFC 3986 + */ + private String percentEncodeRfc3986(String s) { + String out; + try { + out = URLEncoder.encode(s, UTF8_CHARSET) + .replace("+", "%20") + .replace("*", "%2A") + .replace("%7E", "~"); + } catch (UnsupportedEncodingException e) { + out = s; + } + return out; + } + + /** + * Takes a query string, separates the constituent name-value pairs + * and stores them in a hashmap. + * + * @param queryString + * @return + */ + private Map createParameterMap(String queryString) { + Map map = new HashMap(); + String[] pairs = queryString.split("&"); + + for (String pair: pairs) { + if (pair.length() < 1) { + continue; + } + + String[] tokens = pair.split("=",2); + for(int j=0; j