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;
17
18 import net.sf.beep4j.internal.util.Assert;
19 import net.sf.beep4j.internal.util.ByteUtil;
20
21 /**
22 * Represents a profile as seen inside a start channel request.
23 *
24 * @author Simon Raess
25 */
26 public final class ProfileInfo {
27
28 private final String uri;
29
30 private boolean base64;
31
32 private byte[] data;
33
34 private String content;
35
36 public ProfileInfo(String uri) {
37 Assert.notNull("uri", uri);
38 this.uri = uri;
39 }
40
41 public ProfileInfo(String uri, byte[] data) {
42 this(uri);
43 this.base64 = true;
44 this.data = data;
45 }
46
47 public ProfileInfo(String uri, String content) {
48 this(uri);
49 this.base64 = false;
50 this.content = content;
51 }
52
53 /**
54 * Determines whether this ProfileInfo has content.
55 *
56 * @return true iff there is content
57 */
58 public final boolean hasContent() {
59 return data != null || content != null;
60 }
61
62 /**
63 * The profile URI, which should uniquely identify the profile.
64 *
65 * @return the URI of the profile
66 */
67 public final String getUri() {
68 return uri;
69 }
70
71 /**
72 * Determines whether the body was (or should be) base 64 encoded.
73 *
74 * @return true if base 64 encoded
75 */
76 public final boolean isBase64Encoded() {
77 return base64;
78 }
79
80 /**
81 * Tries to retrieve a the textual content. If no textual content
82 * is present an IllegalStateException is thrown.
83 *
84 * @return a textual content
85 * @throws IllegalStateException if there is no textual content
86 */
87 public final String getContent() {
88 if (content == null) {
89 throw new IllegalStateException("ProfileInfo does not have textual content");
90 }
91 return content;
92 }
93
94 /**
95 * Tries to retrieve the binary content. If no binary content is present
96 * an IllegalStateException is thrown. The content returned is base64 decoded.
97 *
98 * @return the base64 decoded initialization data
99 * @throws IllegalStateException if there is no binary data
100 */
101 public final byte[] getBinaryContent() {
102 if (data == null) {
103 throw new IllegalStateException("ProfileInfo does not have binary data");
104 }
105 return data;
106 }
107
108 /**
109 * Append the ProfileInfo content to the StringBuilder.
110 *
111 * @param appendable the StringBuilder to which the content is appended
112 */
113 public final void appendTo(StringBuilder builder) {
114 if (hasContent()) {
115 if (isBase64Encoded()) {
116 builder.append(ByteUtil.base64Encode(data));
117 } else {
118 builder.append(content);
119 }
120 }
121 }
122
123 @Override
124 public String toString() {
125 return "<profile uri='" + uri + "' />";
126 }
127
128 }