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

COVERAGE SUMMARY FOR SOURCE FILE [BindBucketLoggingToXmlPayload.java]

nameclass, %method, %block, %line, %
BindBucketLoggingToXmlPayload.java100% (1/1)100% (3/3)68%  (114/168)74%  (22.8/31)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class BindBucketLoggingToXmlPayload100% (1/1)100% (3/3)68%  (114/168)74%  (22.8/31)
bindToRequest (HttpRequest, Object): HttpRequest 100% (1/1)64%  (29/45)73%  (8/11)
generateBuilder (BucketLogging): XMLBuilder 100% (1/1)68%  (82/120)73%  (13.8/19)
BindBucketLoggingToXmlPayload (): void 100% (1/1)100% (3/3)100% (1/1)

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.s3.binders;
20 
21import java.util.Properties;
22 
23import javax.inject.Singleton;
24import javax.ws.rs.core.MediaType;
25import javax.xml.parsers.FactoryConfigurationError;
26import javax.xml.parsers.ParserConfigurationException;
27 
28import org.jclouds.s3.domain.BucketLogging;
29import org.jclouds.s3.domain.AccessControlList.CanonicalUserGrantee;
30import org.jclouds.s3.domain.AccessControlList.EmailAddressGrantee;
31import org.jclouds.s3.domain.AccessControlList.Grant;
32import org.jclouds.s3.domain.AccessControlList.GroupGrantee;
33import org.jclouds.s3.reference.S3Constants;
34import org.jclouds.http.HttpRequest;
35import org.jclouds.rest.Binder;
36 
37import com.google.common.base.Throwables;
38import com.jamesmurty.utils.XMLBuilder;
39 
40/**
41 * 
42 * @author Adrian Cole
43 */
44@Singleton
45public class BindBucketLoggingToXmlPayload implements Binder {
46   @Override
47   public <R extends HttpRequest> R bindToRequest(R request, Object payload) {
48      BucketLogging from = (BucketLogging) payload;
49      Properties outputProperties = new Properties();
50      outputProperties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes");
51      try {
52         String stringPayload = generateBuilder(from).asString(outputProperties);
53         request.setPayload(stringPayload);
54         request.getPayload().getContentMetadata().setContentType(MediaType.TEXT_XML);
55      } catch (Exception e) {
56         Throwables.propagateIfPossible(e);
57         throw new RuntimeException("error transforming bucketLogging: " + from, e);
58      }
59      return request;
60   }
61 
62   protected XMLBuilder generateBuilder(BucketLogging bucketLogging) throws ParserConfigurationException,
63         FactoryConfigurationError {
64      XMLBuilder rootBuilder = XMLBuilder.create("BucketLoggingStatus")
65            .attr("xmlns", S3Constants.S3_REST_API_XML_NAMESPACE).e("LoggingEnabled");
66      rootBuilder.e("TargetBucket").t(bucketLogging.getTargetBucket());
67      rootBuilder.e("TargetPrefix").t(bucketLogging.getTargetPrefix());
68      XMLBuilder grantsBuilder = rootBuilder.elem("TargetGrants");
69      for (Grant grant : bucketLogging.getTargetGrants()) {
70         XMLBuilder grantBuilder = grantsBuilder.elem("Grant");
71         XMLBuilder granteeBuilder = grantBuilder.elem("Grantee").attr("xmlns:xsi",
72               "http://www.w3.org/2001/XMLSchema-instance");
73 
74         if (grant.getGrantee() instanceof GroupGrantee) {
75            granteeBuilder.attr("xsi:type", "Group").elem("URI").text(grant.getGrantee().getIdentifier());
76         } else if (grant.getGrantee() instanceof CanonicalUserGrantee) {
77            CanonicalUserGrantee grantee = (CanonicalUserGrantee) grant.getGrantee();
78            granteeBuilder.attr("xsi:type", "CanonicalUser").elem("ID").text(grantee.getIdentifier()).up();
79            if (grantee.getDisplayName() != null) {
80               granteeBuilder.elem("DisplayName").text(grantee.getDisplayName());
81            }
82         } else if (grant.getGrantee() instanceof EmailAddressGrantee) {
83            granteeBuilder.attr("xsi:type", "AmazonCustomerByEmail").elem("EmailAddress")
84                  .text(grant.getGrantee().getIdentifier());
85         }
86         grantBuilder.elem("Permission").text(grant.getPermission().toString());
87      }
88      return grantsBuilder;
89   }
90 
91}

[all classes][org.jclouds.s3.binders]
EMMA 2.0.5312 (C) Vladimir Roubtsov