This project has retired. For details please refer to its
Attic page.
ValidateCustodyTransfer xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.juddi.validation;
18
19 import java.io.UnsupportedEncodingException;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.Date;
23 import java.util.List;
24 import java.util.Vector;
25
26 import javax.persistence.EntityManager;
27 import javax.persistence.Query;
28 import static org.apache.juddi.api.impl.AuthenticatedService.UTF8;
29
30 import org.apache.juddi.model.TransferTokenKey;
31 import org.apache.juddi.model.UddiEntity;
32 import org.apache.juddi.model.UddiEntityPublisher;
33 import org.apache.juddi.query.util.DynamicQuery;
34 import org.apache.juddi.v3.error.ErrorMessage;
35 import org.apache.juddi.v3.error.FatalErrorException;
36 import org.apache.juddi.v3.error.InvalidKeyPassedException;
37 import org.apache.juddi.v3.error.InvalidValueException;
38 import org.apache.juddi.v3.error.TokenAlreadyExistsException;
39 import org.apache.juddi.v3.error.TransferNotAllowedException;
40 import org.apache.juddi.v3.error.UserMismatchException;
41 import org.apache.juddi.v3.error.ValueNotAllowedException;
42 import org.uddi.custody_v3.DiscardTransferToken;
43 import org.uddi.custody_v3.KeyBag;
44 import org.uddi.custody_v3.TransferEntities;
45 import org.uddi.v3_service.DispositionReportFaultMessage;
46
47
48
49
50 public class ValidateCustodyTransfer extends ValidateUDDIApi {
51
52 public ValidateCustodyTransfer(UddiEntityPublisher publisher) {
53 super(publisher);
54 }
55
56 public void validateDiscardTransferToken(EntityManager em, DiscardTransferToken body) throws DispositionReportFaultMessage {
57
58 if (body == null) {
59 throw new FatalErrorException(new ErrorMessage("errors.NullInput"));
60 }
61
62 KeyBag keyBag = body.getKeyBag();
63
64
65 if (body.getTransferToken() == null && keyBag == null) {
66 throw new FatalErrorException(new ErrorMessage("errors.discardtransfertoken.NoInput"));
67 }
68
69 if (keyBag != null) {
70 List<String> keyList = keyBag.getKey();
71 if (keyList == null || keyList.size() == 0) {
72 throw new ValueNotAllowedException(new ErrorMessage("errors.keybag.NoInput"));
73 }
74
75
76 int i = 0;
77 for (String key : keyList) {
78
79 key = key.toLowerCase();
80 keyList.set(i, key);
81
82 UddiEntity uddiEntity = em.find(UddiEntity.class, key);
83
84
85
86 if (uddiEntity != null) {
87 if (!publisher.isOwner(uddiEntity)) {
88 throw new UserMismatchException(new ErrorMessage("errors.usermismatch.InvalidOwner", key));
89 }
90
91 }
92
93 i++;
94 }
95
96 }
97
98 }
99
100 public void validateGetTransferToken(EntityManager em, KeyBag keyBag) throws DispositionReportFaultMessage {
101
102
103 if (keyBag == null) {
104 throw new FatalErrorException(new ErrorMessage("errors.NullInput"));
105 }
106
107 List<String> keyList = keyBag.getKey();
108 if (keyList == null || keyList.size() == 0) {
109 throw new ValueNotAllowedException(new ErrorMessage("errors.keybag.NoInput"));
110 }
111
112
113 Vector<DynamicQuery.Parameter> params = new Vector<DynamicQuery.Parameter>(0);
114 int i = 0;
115 for (String key : keyList) {
116
117 key = key.toLowerCase();
118 keyList.set(i, key);
119
120 UddiEntity uddiEntity = em.find(UddiEntity.class, key);
121
122 if (uddiEntity == null) {
123 throw new InvalidKeyPassedException(new ErrorMessage("errors.invalidkey.EntityNotFound", key));
124 }
125
126
127 if (!(uddiEntity instanceof org.apache.juddi.model.BusinessEntity)
128 && !(uddiEntity instanceof org.apache.juddi.model.Tmodel)) {
129 throw new InvalidKeyPassedException(new ErrorMessage("errors.gettransfertoken.InvalidEntity", key));
130 }
131
132 if (!publisher.isOwner(uddiEntity)) {
133 throw new UserMismatchException(new ErrorMessage("errors.usermismatch.InvalidOwner", key));
134 }
135
136
137 DynamicQuery.Parameter param = new DynamicQuery.Parameter("UPPER(ttk.entityKey)",
138 key.toUpperCase(),
139 DynamicQuery.PREDICATE_EQUALS);
140 params.add(param);
141
142 }
143
144
145 DynamicQuery checkKeysQry = new DynamicQuery();
146 checkKeysQry.append("select ttk.entityKey from TransferTokenKey ttk ");
147 checkKeysQry.WHERE().pad().appendGroupedOr(params.toArray(new DynamicQuery.Parameter[0]));
148
149 Query qry = checkKeysQry.buildJPAQuery(em);
150 List<?> obj = qry.getResultList();
151 if (obj != null && obj.size() > 0) {
152 throw new TokenAlreadyExistsException(new ErrorMessage("errors.gettransfertoken.KeyExists", (String) obj.get(0)));
153 }
154
155 }
156
157 public void validateTransferLocalEntities(EntityManager em, String transferTokenId, List<String> apiKeyList) throws DispositionReportFaultMessage {
158 org.apache.juddi.model.TransferToken modelTransferToken = em.find(org.apache.juddi.model.TransferToken.class, transferTokenId);
159
160 if (modelTransferToken == null) {
161 throw new TransferNotAllowedException(new ErrorMessage("errors.transferentities.TokenNotFound", transferTokenId));
162 }
163
164 Date now = new Date();
165 if (now.after(modelTransferToken.getExpirationDate())) {
166 throw new TransferNotAllowedException(new ErrorMessage("errors.transferentities.TokenExpired", transferTokenId));
167 }
168
169 List<TransferTokenKey> transferKeyList = modelTransferToken.getTransferKeys();
170 List<String> modelKeyList = new ArrayList<String>(0);
171 if (transferKeyList != null && transferKeyList.size() > 0) {
172 for (TransferTokenKey ttk : transferKeyList) {
173 modelKeyList.add(ttk.getEntityKey());
174 }
175 }
176
177
178 Collections.sort(apiKeyList);
179 Collections.sort(modelKeyList);
180 int count = 0;
181 if (modelKeyList.size() != apiKeyList.size()) {
182 throw new TransferNotAllowedException(new ErrorMessage("errors.transferentities.KeySizeMismatch"));
183 }
184 for (String key : apiKeyList) {
185
186 key = key.toLowerCase();
187 apiKeyList.set(count, key);
188
189 if (!key.equalsIgnoreCase(modelKeyList.get(count))) {
190 throw new TransferNotAllowedException(new ErrorMessage("errors.transferentities.KeyMismatch", key + " & " + modelKeyList.get(count)));
191 }
192
193 UddiEntity uddiEntity = em.find(UddiEntity.class, key);
194 if (uddiEntity == null) {
195 throw new InvalidKeyPassedException(new ErrorMessage("errors.invalidkey.EntityNotFound", key));
196 }
197 count++;
198 }
199
200 }
201
202
203
204
205
206
207
208
209
210
211 public boolean validateTransferEntities(EntityManager em, TransferEntities body) throws DispositionReportFaultMessage {
212
213 boolean ret = true;
214
215 if (body == null) {
216 throw new FatalErrorException(new ErrorMessage("errors.NullInput"));
217 }
218
219 org.uddi.custody_v3.TransferToken apiTransferToken = body.getTransferToken();
220 if (apiTransferToken == null) {
221 throw new FatalErrorException(new ErrorMessage("errors.transfertoken.NullInput"));
222 }
223
224 KeyBag keyBag = body.getKeyBag();
225 if (keyBag == null) {
226 throw new FatalErrorException(new ErrorMessage("errors.keybag.NullInput"));
227 }
228
229 List<String> apiKeyList = keyBag.getKey();
230 if (apiKeyList == null || apiKeyList.size() == 0) {
231 throw new ValueNotAllowedException(new ErrorMessage("errors.keybag.NoInput"));
232 }
233
234 String transferTokenId = null;
235 try{
236 transferTokenId = new String(apiTransferToken.getOpaqueToken(), UTF8);
237 } catch (UnsupportedEncodingException ex) {
238 throw new InvalidValueException(new ErrorMessage("errors.stringEncoding"));
239 }
240 if (nodeID.equals(apiTransferToken.getNodeID())) {
241 validateTransferLocalEntities(em, transferTokenId, apiKeyList);
242 } else {
243 return false;
244 }
245 return ret;
246 }
247 }