1 | // Copyright (C) 2007 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.plugin; |
16 | |
17 | import java.util.List; |
18 | import com.google.caja.CajaException; |
19 | import com.google.caja.lexer.ExternalReference; |
20 | import com.google.caja.lexer.FetchedData; |
21 | import com.google.caja.reporting.Message; |
22 | import com.google.caja.reporting.MessagePart; |
23 | import com.google.caja.util.Lists; |
24 | |
25 | /** |
26 | * Specifies how the cajoler resolves external resources such as scripts and |
27 | * stylesheets in the code being cajoled. |
28 | * |
29 | * @author mikesamuel@gmail.com |
30 | */ |
31 | public interface UriFetcher { |
32 | |
33 | /** |
34 | * Loads an external resource such as the {@code src} of a {@code script} |
35 | * tag or a stylesheet. |
36 | * |
37 | * @return non-null resource |
38 | * @throws UriFetchException if the resource could not be loaded |
39 | */ |
40 | FetchedData fetch(ExternalReference ref, String mimeType) |
41 | throws UriFetchException; |
42 | |
43 | /** A fetcher that will not load any URI. */ |
44 | public static final UriFetcher NULL_NETWORK = new DataUriFetcher(); |
45 | |
46 | public static class UriFetchException extends CajaException { |
47 | public final ExternalReference ref; |
48 | public final String expectedMimeType; |
49 | |
50 | public UriFetchException( |
51 | ExternalReference ref, String mimeType, Throwable cause) { |
52 | super( |
53 | new Message( |
54 | PluginMessageType.FAILED_TO_LOAD_EXTERNAL_URL, |
55 | ref.getReferencePosition(), |
56 | MessagePart.Factory.valueOf(ref.getUri().toString())), |
57 | cause); |
58 | this.ref = ref; |
59 | this.expectedMimeType = mimeType; |
60 | } |
61 | |
62 | public UriFetchException( |
63 | ExternalReference ref, String mimeType, List<Throwable> causes) { |
64 | this(ref, mimeType, causes.isEmpty() ? null : causes.get(0)); |
65 | } |
66 | |
67 | public UriFetchException(ExternalReference ref, String mimeType) { |
68 | this(ref, mimeType, (Throwable) null); |
69 | } |
70 | } |
71 | |
72 | /** |
73 | * Chains one of more uri fetchers in order and returns the first |
74 | * successfully loaded resource |
75 | */ |
76 | public final static class ChainingUriFetcher { |
77 | public static UriFetcher make(final UriFetcher... fetchers) { |
78 | return new UriFetcher() { |
79 | List<Throwable> causes = Lists.newArrayList(); |
80 | public FetchedData fetch(ExternalReference ref, String mimeType) |
81 | throws UriFetchException { |
82 | for (UriFetcher fetcher : fetchers) { |
83 | try { |
84 | return fetcher.fetch(ref, mimeType); |
85 | } catch (UriFetchException e) { |
86 | causes.add(e); |
87 | } |
88 | } |
89 | // None of the fetchers succeeded |
90 | throw new UriFetchException(ref, mimeType, causes); |
91 | } |
92 | }; |
93 | } |
94 | } |
95 | |
96 | } |