001: /* Copyright 2006 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.security.provider.cas;
007:
008: import java.util.ArrayList;
009: import java.util.Collections;
010: import java.util.Enumeration;
011: import java.util.HashMap;
012: import java.util.List;
013: import java.util.Map;
014:
015: import org.jasig.portal.security.IAdditionalDescriptor;
016: import org.jasig.portal.security.IOpaqueCredentials;
017: import org.jasig.portal.security.IPrincipal;
018: import org.jasig.portal.security.ISecurityContext;
019: import org.jasig.portal.security.PortalSecurityException;
020:
021: /**
022: * A mock-object CAS security context.
023: * This mock object can be configured to mock-out most CAS security context
024: * behaviors and is useful for testing CasConnectionContext (and potentially
025: * other components making use of ICasSecurityContexts).
026: *
027: * This mock object is not threadsafe.
028: */
029: public class CasSecurityContextMock implements ISecurityContext,
030: ICasSecurityContext {
031:
032: private static final long serialVersionUID = 1L;
033:
034: private boolean authenticated = false;
035:
036: /**
037: * A List of String service tokens and/or CasProxyTicketAcquisitionExceptions
038: * and/or RuntimeExceptions which programs the behavior of this object's
039: * response to getCasServiceToken(). For any call of getCasServiceToken(),
040: * this object will respond by returning the first object in this List if
041: * it is a String or null, or throwing the first object in this list if
042: * it is a CasProxyTicketAcquisitionException or a RuntimeException.
043: * This object will then remove the first item in this list and append it to
044: * vendedServiceTokens. If this list is empty, this object will treat it
045: * as if it had contained null.
046: */
047: private List serviceTokensToVend = new ArrayList();
048:
049: /**
050: * A List of responses from getCasServiceToken(), in the order given.
051: */
052: private final List vendedServiceTokens = new ArrayList();
053:
054: /**
055: * A List of the String targets for which service tokens were requested.
056: */
057: private final List serviceTokenTargets = new ArrayList();
058:
059: /**
060: * Map from subcontext name to ISecurityContext value.
061: */
062: private Map subcontexts = new HashMap();
063:
064: public String getCasServiceToken(String target)
065: throws CasProxyTicketAcquisitionException {
066:
067: // record the target
068: this .serviceTokenTargets.add(target);
069:
070: // return or throw the first item in serviceTokensToVend.
071:
072: Object toVend = null;
073:
074: if (!serviceTokensToVend.isEmpty()) {
075: toVend = this .serviceTokensToVend.get(0);
076: this .serviceTokensToVend.remove(0);
077: }
078:
079: // if the list was empty, toVend is still null, as desired.
080:
081: // add the response to the end of the list
082: this .vendedServiceTokens.add(toVend);
083:
084: // return or throw the response
085: if (toVend == null) {
086: return null;
087: } else if (toVend instanceof CasProxyTicketAcquisitionException) {
088: throw (CasProxyTicketAcquisitionException) toVend;
089: } else if (toVend instanceof RuntimeException) {
090: throw (RuntimeException) toVend;
091: } else if (toVend instanceof String) {
092: return (String) toVend;
093: } else {
094: throw new IllegalStateException("Unexpected type: "
095: + toVend);
096: }
097:
098: }
099:
100: public int getAuthType() {
101: return ICasSecurityContext.CAS_AUTHTYPE;
102: }
103:
104: public IPrincipal getPrincipalInstance() {
105: // TODO Auto-generated method stub
106: return null;
107: }
108:
109: public IOpaqueCredentials getOpaqueCredentialsInstance() {
110: // TODO Auto-generated method stub
111: return null;
112: }
113:
114: public void authenticate() throws PortalSecurityException {
115: // TODO Auto-generated method stub
116:
117: }
118:
119: public IPrincipal getPrincipal() {
120: // TODO Auto-generated method stub
121: return null;
122: }
123:
124: public IOpaqueCredentials getOpaqueCredentials() {
125: // TODO Auto-generated method stub
126: return null;
127: }
128:
129: public IAdditionalDescriptor getAdditionalDescriptor() {
130: // TODO Auto-generated method stub
131: return null;
132: }
133:
134: public boolean isAuthenticated() {
135: return this .authenticated;
136: }
137:
138: public ISecurityContext getSubContext(String ctx)
139: throws PortalSecurityException {
140: // TODO Auto-generated method stub
141: return null;
142: }
143:
144: public Enumeration getSubContexts() {
145: return Collections.enumeration(this .subcontexts.values());
146: }
147:
148: public Enumeration getSubContextNames() {
149: return Collections.enumeration(this .subcontexts.keySet());
150: }
151:
152: public void addSubContext(String name, ISecurityContext ctx)
153: throws PortalSecurityException {
154: this .subcontexts.put(name, ctx);
155: }
156:
157: public List getServiceTokensToVend() {
158: return serviceTokensToVend;
159: }
160:
161: public void setServiceTokensToVend(List serviceTokensToVendArg) {
162:
163: List defensiveCopy = new ArrayList();
164: defensiveCopy.addAll(serviceTokensToVendArg);
165:
166: this .serviceTokensToVend = defensiveCopy;
167: }
168:
169: public List getVendedServiceTokens() {
170: return vendedServiceTokens;
171: }
172:
173: public void setAuthenticated(boolean authenticated) {
174: this .authenticated = authenticated;
175: }
176:
177: public List getServiceTokenTargets() {
178: return serviceTokenTargets;
179: }
180:
181: }
|