View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  package com.eviware.soapui.impl.wsdl.panels.teststeps.amf;
13  
14  import com.eviware.soapui.SoapUI;
15  import com.eviware.soapui.model.iface.SubmitContext;
16  
17  import flex.messaging.io.amf.client.exceptions.ClientStatusException;
18  import flex.messaging.io.amf.client.exceptions.ServerStatusException;
19  import flex.messaging.messages.CommandMessage;
20  import flex.messaging.util.Base64.Encoder;
21  
22  public class AMFCredentials
23  {
24  
25  	public static final String DESTINATION = "auth";
26  	private String endpoint;
27  	private String username;
28  	private String password;
29  	private SubmitContext context;
30  	private boolean logedIn;
31  
32  	public AMFCredentials( String endpoint, String username, String password, SubmitContext context )
33  	{
34  		this.endpoint = endpoint;
35  		this.username = username;
36  		this.password = password;
37  		this.context = context;
38  	}
39  
40  	public SoapUIAMFConnection login() throws ClientStatusException, ServerStatusException
41  	{
42  		CommandMessage commandMessage = createLoginCommandMessage();
43  
44  		SoapUIAMFConnection amfConnection = new SoapUIAMFConnection();
45  		amfConnection.connect( endpoint );
46  		amfConnection.call( ( SubmitContext )context, null, commandMessage );
47  		logedIn = true;
48  		return amfConnection;
49  	}
50  
51  	public static void logout( SubmitContext context )
52  	{
53  		SoapUIAMFConnection connection = ( SoapUIAMFConnection )context.getProperty( AMFSubmit.AMF_CONNECTION );
54  		CommandMessage commandMessage = createLogoutCommandMessage();
55  		try
56  		{
57  			connection.call( ( SubmitContext )context, null, commandMessage );
58  		}
59  		catch( ClientStatusException e )
60  		{
61  			SoapUI.logError( e );
62  		}
63  		catch( ServerStatusException e )
64  		{
65  			SoapUI.logError( e );
66  		}
67  		finally
68  		{
69  			connection.close();
70  		}
71  	}
72  	
73  	public void logout()
74  	{
75  		SoapUIAMFConnection connection = ( SoapUIAMFConnection )context.getProperty( AMFSubmit.AMF_CONNECTION );
76  		CommandMessage commandMessage = createLogoutCommandMessage();
77  		try
78  		{
79  			connection.call( ( SubmitContext )context, null, commandMessage );
80  		}
81  		catch( ClientStatusException e )
82  		{
83  			SoapUI.logError( e );
84  		}
85  		catch( ServerStatusException e )
86  		{
87  			SoapUI.logError( e );
88  		}
89  		finally
90  		{
91  			connection.close();
92  		}
93  	}
94  
95  	private CommandMessage createLoginCommandMessage()
96  	{
97  		CommandMessage commandMessage = new CommandMessage();
98  		commandMessage.setOperation( CommandMessage.LOGIN_OPERATION );
99  
100 		String credString = username + ":" + password;
101 		Encoder encoder = new Encoder( credString.length() );
102 		encoder.encode( credString.getBytes() );
103 
104 		commandMessage.setBody( encoder.drain() );
105 		commandMessage.setDestination( DESTINATION );
106 		return commandMessage;
107 	}
108 
109 	private static CommandMessage createLogoutCommandMessage()
110 	{
111 		CommandMessage commandMessage = new CommandMessage();
112 		commandMessage.setOperation( CommandMessage.LOGOUT_OPERATION );
113 		commandMessage.setDestination( DESTINATION );
114 		return commandMessage;
115 	}
116 
117 	public String getEndpoint()
118 	{
119 		return endpoint;
120 	}
121 
122 	public String getUsername()
123 	{
124 		return username;
125 	}
126 
127 	public String getPassword()
128 	{
129 		return password;
130 	}
131 
132 	public SubmitContext getContext()
133 	{
134 		return context;
135 	}
136 
137 	public void setLogedIn( boolean logedIn )
138 	{
139 		this.logedIn = logedIn;
140 	}
141 
142 	public boolean isLoggedIn()
143 	{
144 		return logedIn;
145 	}
146 
147 }