1 package fr.in2p3.jsaga.engine.factories;
2
3 import fr.in2p3.jsaga.adaptor.ClientAdaptor;
4 import fr.in2p3.jsaga.adaptor.base.usage.Usage;
5 import fr.in2p3.jsaga.adaptor.job.JobAdaptor;
6 import fr.in2p3.jsaga.adaptor.security.SecurityCredential;
7 import fr.in2p3.jsaga.engine.descriptors.SecurityAdaptorDescriptor;
8 import fr.in2p3.jsaga.impl.context.ContextImpl;
9 import org.ogf.saga.context.Context;
10 import org.ogf.saga.error.*;
11 import org.ogf.saga.url.URL;
12
13 import java.util.*;
14
15
16
17
18
19
20
21
22
23
24
25
26
27 public class ServiceAdaptorFactory {
28 public static Map getAttributes(URL url, ContextImpl context, Map adaptorDefaults, String serviceType) throws NotImplementedException, BadParameterException, NoSuccessException {
29
30 Map attributes = new HashMap();
31
32
33 String scheme = context.getSchemeFromAlias(url.getScheme());
34 Properties serviceConfig = context.getServiceConfig(serviceType, scheme);
35 attributes.putAll(adaptorDefaults);
36 if (serviceConfig != null) {
37 attributes.putAll(serviceConfig);
38 }
39
40
41 String query = url.getQuery();
42 if (query != null) {
43 String[] pairs = query.split("&");
44 for (int i=0; pairs!=null && i<pairs.length; i++) {
45 String[] pair = pairs[i].split("=", 2);
46 switch (pair.length) {
47 case 1:
48 attributes.put(pair[0], null);
49 break;
50 case 2:
51 attributes.put(pair[0], pair[1]);
52 break;
53 default:
54 throw new BadParameterException("Bad query in URL: "+url);
55 }
56 }
57 }
58 return attributes;
59 }
60
61 protected static SecurityCredential getCredential(URL url, ContextImpl context, ClientAdaptor adaptor) throws NotImplementedException, AuthenticationFailedException, BadParameterException, TimeoutException, NoSuccessException {
62 if (context != null) {
63 if (SecurityAdaptorDescriptor.isSupported(context.getCredentialClass(), adaptor.getSupportedSecurityCredentialClasses())) {
64 try {
65 return context.getCredential();
66 } catch (IncorrectStateException e) {
67 try {
68 throw new NoSuccessException("Invalid security context: "+context.getAttribute(Context.TYPE), e);
69 } catch (SagaException e2) {
70 throw new NoSuccessException("Invalid security context: "+e2.getMessage(), e);
71 }
72 }
73 } else if (SecurityAdaptorDescriptor.isSupportedNoContext(adaptor.getSupportedSecurityCredentialClasses())) {
74 return null;
75 } else {
76 throw new AuthenticationFailedException("Security context class '"+context.getCredentialClass().getName()+"' not supported for protocol: "+url.getScheme());
77 }
78 } else if (SecurityAdaptorDescriptor.isSupportedNoContext(adaptor.getSupportedSecurityCredentialClasses())) {
79 return null;
80 } else {
81 throw new AuthenticationFailedException("No security context configured for URL: "+url);
82 }
83 }
84
85 protected void checkAttributesValidity(Map<String, ?> attributes, Usage usage) throws BadParameterException {
86 if (attributes.isEmpty()) return;
87 Set<String> adaptorAttributes;
88 if (usage != null) {
89 adaptorAttributes = usage.getKeys();
90 } else {
91 adaptorAttributes = new HashSet<String>();
92 }
93 adaptorAttributes.add(JobAdaptor.CHECK_AVAILABILITY);
94 Iterator<String> i = attributes.keySet().iterator();
95 while (i.hasNext()) {
96 Object attr = i.next();
97 if (!adaptorAttributes.contains(attr)) {
98 throw new BadParameterException("Invalid attribute: " + attr.toString());
99 }
100 }
101 }
102 }