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

COVERAGE SUMMARY FOR SOURCE FILE [BindACLToXMLPayload.java]

nameclass, %method, %block, %line, %
BindACLToXMLPayload.java100% (1/1)100% (3/3)67%  (122/181)77%  (26.2/34)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class BindACLToXMLPayload100% (1/1)100% (3/3)67%  (122/181)77%  (26.2/34)
bindToRequest (HttpRequest, Object): HttpRequest 100% (1/1)64%  (29/45)73%  (8/11)
generateBuilder (AccessControlList): XMLBuilder 100% (1/1)68%  (90/133)78%  (17.2/22)
BindACLToXMLPayload (): 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.AccessControlList;
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 James Murty
43 */
44@Singleton
45public class BindACLToXMLPayload implements Binder {
46   @Override
47   public <R extends HttpRequest> R bindToRequest(R request, Object payload) {
48      AccessControlList from = (AccessControlList) 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 acl: " + from, e);
58      }
59      return request;
60   }
61 
62   protected XMLBuilder generateBuilder(AccessControlList acl) throws ParserConfigurationException,
63         FactoryConfigurationError {
64      XMLBuilder rootBuilder = XMLBuilder.create("AccessControlPolicy").attr("xmlns",
65            S3Constants.S3_REST_API_XML_NAMESPACE);
66      if (acl.getOwner() != null) {
67         XMLBuilder ownerBuilder = rootBuilder.elem("Owner");
68         ownerBuilder.elem("ID").text(acl.getOwner().getId()).up();
69         if (acl.getOwner().getDisplayName() != null) {
70            ownerBuilder.elem("DisplayName").text(acl.getOwner().getDisplayName()).up();
71         }
72      }
73      XMLBuilder grantsBuilder = rootBuilder.elem("AccessControlList");
74      for (Grant grant : acl.getGrants()) {
75         XMLBuilder grantBuilder = grantsBuilder.elem("Grant");
76         XMLBuilder granteeBuilder = grantBuilder.elem("Grantee").attr("xmlns:xsi",
77               "http://www.w3.org/2001/XMLSchema-instance");
78 
79         if (grant.getGrantee() instanceof GroupGrantee) {
80            granteeBuilder.attr("xsi:type", "Group").elem("URI").text(grant.getGrantee().getIdentifier());
81         } else if (grant.getGrantee() instanceof CanonicalUserGrantee) {
82            CanonicalUserGrantee grantee = (CanonicalUserGrantee) grant.getGrantee();
83            granteeBuilder.attr("xsi:type", "CanonicalUser").elem("ID").text(grantee.getIdentifier()).up();
84            if (grantee.getDisplayName() != null) {
85               granteeBuilder.elem("DisplayName").text(grantee.getDisplayName());
86            }
87         } else if (grant.getGrantee() instanceof EmailAddressGrantee) {
88            granteeBuilder.attr("xsi:type", "AmazonCustomerByEmail").elem("EmailAddress")
89                  .text(grant.getGrantee().getIdentifier());
90         }
91         grantBuilder.elem("Permission").text(grant.getPermission().toString());
92      }
93      return grantsBuilder;
94   }
95 
96}

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