1 | // Copyright 2008 Google Inc. All Rights Reserved. |
2 | // Licensed under the Apache License, Version 2.0 (the "License"); |
3 | // you may not use this file except in compliance with the License. |
4 | // You may obtain a copy of the License at |
5 | // |
6 | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | // |
8 | // Unless required by applicable law or agreed to in writing, software |
9 | // distributed under the License is distributed on an "AS IS" BASIS, |
10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
11 | // See the License for the specific language governing permissions and |
12 | // limitations under the License. |
13 | |
14 | package com.google.caja.service; |
15 | |
16 | import com.google.caja.lexer.FetchedData; |
17 | import com.google.caja.reporting.MessagePart; |
18 | import com.google.caja.reporting.MessageQueue; |
19 | import com.google.caja.reporting.MessageType; |
20 | import com.google.caja.util.Pair; |
21 | |
22 | import java.io.IOException; |
23 | import java.io.OutputStream; |
24 | import java.net.URI; |
25 | import java.util.List; |
26 | |
27 | /** |
28 | * Retrieves image objects and returns them unchecked |
29 | * to the requester |
30 | * |
31 | * @author jasvir@google.com (Jasvir Nagra) |
32 | */ |
33 | public class ImageHandler implements ContentHandler { |
34 | |
35 | public boolean canHandle(URI uri, CajolingService.Transform transform, |
36 | List<CajolingService.Directive> directives, |
37 | String inputContentType, |
38 | ContentTypeCheck checker) { |
39 | return checker.check("image/*", inputContentType); |
40 | } |
41 | |
42 | public Pair<String, String> apply(URI uri, |
43 | CajolingService.Transform trans, |
44 | List<CajolingService.Directive> directives, |
45 | ContentHandlerArgs args, |
46 | String inputContentType, |
47 | ContentTypeCheck checker, |
48 | FetchedData input, |
49 | OutputStream response, |
50 | MessageQueue mq) { |
51 | try { |
52 | response.write(input.getByteContent()); |
53 | return Pair.pair(inputContentType, ""); |
54 | } catch (IOException e) { |
55 | mq.addMessage( |
56 | MessageType.IO_ERROR, |
57 | MessagePart.Factory.valueOf(e.getMessage())); |
58 | return null; |
59 | } |
60 | } |
61 | } |