1 | // Copyright (C) 2009 Google Inc. |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package com.google.caja.ancillary.servlet; |
16 | |
17 | import com.google.caja.reporting.MessageQueue; |
18 | import com.google.caja.util.Lists; |
19 | import com.google.caja.util.Pair; |
20 | |
21 | import java.util.List; |
22 | |
23 | /** |
24 | * Abstracts away a servlet response so the servlet can be easily tested. |
25 | * |
26 | * @author mikesamuel@gmail.com |
27 | */ |
28 | final class Result { |
29 | /** A response code, e.g. 200 for OK. */ |
30 | final int status; |
31 | /** The response content. */ |
32 | final Content content; |
33 | /** Any messages that should be reported to the user. */ |
34 | final MessageQueue mq; |
35 | /** Extra response headers. */ |
36 | final List<Pair<String, String>> headers = Lists.newArrayList(); |
37 | |
38 | Result(int status, Content content, MessageQueue mq) { |
39 | if (content == null) { throw new NullPointerException(); } |
40 | this.status = status; |
41 | this.content = content; |
42 | this.mq = mq; |
43 | if (content.type != null) { |
44 | headers.add(Pair.pair("Content-type", getContentType())); |
45 | } |
46 | } |
47 | |
48 | String getContentType() { |
49 | if (content.type == null) { return null; } |
50 | String contentType = content.type.mimeType; |
51 | return content.type.isText ? contentType + "; charset=UTF-8" : contentType; |
52 | } |
53 | } |