1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }