View Javadoc

1   /*
2    *  Copyright 2006 Simon Raess
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package net.sf.beep4j.internal;
17  
18  import net.sf.beep4j.ChannelHandler;
19  import net.sf.beep4j.ProfileInfo;
20  import net.sf.beep4j.StartChannelRequest;
21  import net.sf.beep4j.internal.util.Assert;
22  
23  final class DefaultStartChannelRequest implements StartChannelRequest {
24  	
25  	private final ProfileInfo[] profiles;
26  	
27  	private StartChannelResponse response;
28  	
29  	public DefaultStartChannelRequest(ProfileInfo[] profiles) {
30  		Assert.notNull("profiles", profiles);
31  		this.profiles = profiles.clone();
32  	}
33  	
34  	public StartChannelResponse getResponse() {
35  		if (response == null) {
36  			response = StartChannelResponse.createCancelledResponse(
37  					550, "all requested profiles are not supported");
38  		}
39  		return response;
40  	}
41  	
42  	public ProfileInfo[] getProfiles() {
43  		return profiles.clone();
44  	}
45  	
46  	public boolean hasProfile(String profileUri) {
47  		for (int i = 0; i < profiles.length; i++) {
48  			if (profileUri.equals(profiles[i].getUri())) {
49  				return true;
50  			}
51  		}
52  		return false;
53  	}
54  	
55  	public ProfileInfo getProfile(String profileUri) {
56  		for (int i = 0; i < profiles.length; i++) {
57  			if (profileUri.equals(profiles[i].getUri())) {
58  				return profiles[i];
59  			}
60  		}
61  		throw new IllegalArgumentException("there is no ProfileInfo object matching "
62  				+ "the passed in profile uri: '" + profileUri + "'");
63  	}
64  	
65  	public void cancel(int code, String message) {
66  		if (response != null) {
67  			throw new IllegalStateException("StartChannelRequest is already either "
68  					+ "cancelled or accepted");
69  		}
70  		response = StartChannelResponse.createCancelledResponse(
71  					code, message);
72  	}
73  
74  	public void selectProfile(ProfileInfo profile, ChannelHandler handler) {
75  		Assert.notNull("profile", profile);
76  		Assert.notNull("handler", handler);
77  		if (response != null) {
78  			throw new IllegalStateException("StartChannelRequest is already either "
79  					+ "cancelled or accepted");
80  		}
81  		checkProfile(profile.getUri());
82  		response = StartChannelResponse.createSuccessResponse(profile, handler);
83  	}
84  	
85  	private void checkProfile(String profile) {
86  		for (int i = 0; i < profiles.length; i++) {
87  			ProfileInfo initial = profiles[i];
88  			if (profile.equals(initial.getUri())) {
89  				return;
90  			}
91  		}
92  		throw new IllegalArgumentException("cannot select profile: " + profile
93  				+ " (not found in list of acceptable profiles)");
94  	}
95  	
96  }