1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.ci.comm.command;
17
18 import java.util.Map;
19
20 import org.apache.commons.httpclient.HttpMethodBase;
21 import org.apache.commons.lang.StringUtils;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import de.smartics.ci.comm.LogHelper;
26 import de.smartics.ci.comm.LogLevel;
27
28
29
30
31
32
33 public abstract class AbstractCommand<T extends Command<T>> implements
34 Command<T>
35 {
36
37
38
39
40
41
42
43
44 protected static final int ERROR_CATEGORY_DESTINGUISHER = 100;
45
46
47
48
49 private static final Logger LOG = LoggerFactory
50 .getLogger(AbstractCommand.class);
51
52
53
54
55
56
57 protected LogLevel logLevel;
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 public final void setLogLevel(final LogLevel logLevel)
77 {
78 this.logLevel = logLevel;
79 }
80
81
82
83
84
85
86
87
88
89 protected void checkParameterNotBlank(final String... params)
90 throws CommandException
91 {
92 if (params.length > 0)
93 {
94 for (final String param : params)
95 {
96 if (StringUtils.isBlank(param))
97 {
98 throw new CommandException("Parameter may not be blank!");
99 }
100 }
101 }
102 }
103
104
105
106
107
108
109
110 protected void checkResult(final int statusCode)
111 throws InvalidRequestException
112 {
113 if (statusCode / ERROR_CATEGORY_DESTINGUISHER != 2)
114 {
115 if (statusCode / ERROR_CATEGORY_DESTINGUISHER == 3)
116 {
117 LogHelper.logError(LOG, logLevel, "found redirect!");
118 }
119 LogHelper.logError(LOG, logLevel, "CI Fehler: " + statusCode);
120 throw new InvalidRequestException("This request is invalid, statusCode: "
121 + statusCode);
122 }
123 }
124
125
126
127
128
129
130
131
132 protected void checkResultRegardingRedirect(final int statusCode)
133 throws InvalidRequestException
134 {
135 if (statusCode / ERROR_CATEGORY_DESTINGUISHER == 3)
136 {
137 LogHelper.logInfo(LOG, logLevel, "Redirecting");
138 }
139 else
140 {
141 checkResult(statusCode);
142 }
143 }
144
145
146
147
148
149
150
151 protected String buildQueryString(final Map<String, String> parameter)
152 {
153 final StringBuilder builder = new StringBuilder();
154 boolean isFirstParameter = true;
155
156 for (final Map.Entry<String, String> entry : parameter.entrySet())
157 {
158 final String delimiter = fetchDelimiter(isFirstParameter);
159 builder.append(delimiter);
160 final String key = entry.getKey();
161 final String value = entry.getValue();
162 builder.append(key + "=" + value);
163 isFirstParameter = false;
164 }
165 return builder.toString();
166 }
167
168
169
170
171
172
173
174
175 protected String fetchDelimiter(final boolean isFirstParameter)
176 {
177 if (isFirstParameter)
178 {
179 return "";
180 }
181 else
182 {
183 return "&";
184 }
185 }
186
187
188
189
190
191
192 protected void addRequestHeaders(final HttpMethodBase method)
193 {
194 method.addRequestHeader("Content-Type", "application/xml; charset=UTF-8");
195 }
196
197
198
199 }