001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.commons.io.res.type.s3; 020 021import java.util.ArrayList; 022import java.util.Iterator; 023import java.util.LinkedHashMap; 024import java.util.List; 025import java.util.Map; 026 027public class AccessControlPolicy { 028 029 private String id; 030 private String displayName; 031 032 private List<AccessControl> accessControlList=new ArrayList<AccessControl>(); 033 034 /** 035 * @param accessControlList the accessControlList to set 036 */ 037 public void setAccessControlList(List<AccessControl> accessControlList) { 038 this.accessControlList = accessControlList; 039 } 040 /** 041 * @return the id 042 */ 043 public String getId() { 044 return id; 045 } 046 /** 047 * @param id the id to set 048 */ 049 public void setId(String id) { 050 this.id = id; 051 } 052 /** 053 * @return the displayName 054 */ 055 public String getDisplayName() { 056 return displayName; 057 } 058 /** 059 * @param displayName the displayName to set 060 */ 061 public void setDisplayName(String displayName) { 062 this.displayName = displayName; 063 } 064 /** 065 * @return the accessControlList 066 */ 067 public List<AccessControl> getAccessControlList() { 068 return accessControlList; 069 } 070 071 072 073 public String toString(){ 074 return toXMLString(); 075 } 076 077 public String toXMLString(){ 078 StringBuilder sb=new StringBuilder("<AccessControlPolicy xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">\n"); 079 080 // Owner 081 sb.append("\t<Owner>\n"); 082 sb.append("\t\t<ID>"+getId()+"</ID>\n"); 083 sb.append("\t\t<DisplayName>"+getDisplayName()+"</DisplayName>\n"); 084 sb.append("\t</Owner>\n"); 085 086 // ACL 087 sb.append("\t<AccessControlList>\n"); 088 AccessControl ac; 089 Iterator<AccessControl> it = accessControlList.iterator(); 090 while(it.hasNext()){ 091 ac=it.next(); 092 sb.append("\t\t<Grant>\n"); 093 094 // Grantee 095 sb.append("\t\t\t<Grantee xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\""+AccessControl.toType(ac.getType(),"Group")+"\">\n"); 096 097 switch(ac.getType()){ 098 case AccessControl.TYPE_CANONICAL_USER: 099 sb.append("\t\t\t\t<ID>"+ac.getId()+"</ID>\n"); 100 sb.append("\t\t\t\t<DisplayName>"+ac.getDisplayName()+"</DisplayName>\n"); 101 break; 102 case AccessControl.TYPE_GROUP: 103 sb.append("\t\t\t\t<URI>"+ac.getUri()+"</URI>\n"); 104 break; 105 case AccessControl.TYPE_EMAIL: 106 sb.append("\t\t\t\t<EmailAddress>"+ac.getEmail()+"</EmailAddress>\n"); 107 break; 108 } 109 110 111 112 113 114 115 sb.append("\t\t\t</Grantee>\n"); 116 117 // Permission 118 sb.append("\t\t\t<Permission>"+ac.getPermission()+"</Permission>\n"); 119 120 sb.append("\t\t</Grant>\n"); 121 } 122 sb.append("\t</AccessControlList>\n"); 123 sb.append("</AccessControlPolicy>"); 124 125 return sb.toString(); 126 } 127 128 public static void removeDuplicates(List<AccessControl> acl){ 129 Map<String,AccessControl> map=new LinkedHashMap<String,AccessControl>(); 130 Iterator<AccessControl> it = acl.iterator(); 131 132 while(it.hasNext()){ 133 AccessControl ac = it.next(); 134 map.put(ac.hash(),ac); 135 } 136 137 acl.clear(); 138 it = map.values().iterator(); 139 while(it.hasNext()){ 140 AccessControl ac = it.next(); 141 acl.add(ac); 142 } 143 144 } 145 146}