summaryrefslogtreecommitdiffstats
path: root/server/src/com/vaadin/server/ResourceReference.java
blob: 94b9f1ac9aa4645736f510c2e4b8151a30b8e72b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* 
 * Copyright 2011 Vaadin Ltd.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.vaadin.server;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import com.vaadin.shared.ApplicationConstants;
import com.vaadin.shared.communication.URLReference;

public class ResourceReference extends URLReference {

    private final Resource resource;
    private final ClientConnector connector;
    private final String key;

    public ResourceReference(Resource resource, ClientConnector connector,
            String key) {
        this.resource = resource;
        this.connector = connector;
        this.key = key;
    }

    public Resource getResource() {
        return resource;
    }

    @Override
    public String getURL() {
        if (resource instanceof ExternalResource) {
            return ((ExternalResource) resource).getURL();
        } else if (resource instanceof ConnectorResource) {
            ConnectorResource connectorResource = (ConnectorResource) resource;

            GlobalResourceHandler globalResourceHandler = connector.getUI()
                    .getSession().getGlobalResourceHandler(false);
            if (globalResourceHandler != null) {
                String uri = globalResourceHandler.getUri(connector,
                        connectorResource);
                if (uri != null && !uri.isEmpty()) {
                    return uri;
                }
            }

            // app://APP/connector/[uiid]/[cid]/[key]/[filename]
            String prefix = key;
            String filename = connectorResource.getFilename();
            if (filename != null && !filename.isEmpty()) {
                prefix += '/' + filename;
            }
            String uri = getConnectorResourceBase(prefix, connector);
            return uri;
        } else if (resource instanceof ThemeResource) {
            final String uri = "theme://"
                    + ((ThemeResource) resource).getResourceId();
            return uri;
        } else {
            throw new RuntimeException(getClass().getSimpleName()
                    + " does not support resources of type: "
                    + resource.getClass().getName());
        }

    }

    private static String getConnectorResourceBase(String filename,
            ClientConnector connector) {
        String uri = ApplicationConstants.APP_PROTOCOL_PREFIX
                + ApplicationConstants.APP_PATH + '/'
                + ConnectorResource.CONNECTOR_PATH + '/'
                + connector.getUI().getUIId() + '/'
                + connector.getConnectorId() + '/' + encodeFileName(filename);
        return uri;
    }

    public static String encodeFileName(String filename) {
        // #7738 At least Tomcat and JBoss refuses requests containing
        // encoded slashes or backslashes in URLs. Application resource URLs
        // should really be passed in another way than as part of the path
        // in the future.
        return urlEncode(filename).replace("%2F", "/").replace("%5C", "\\");
    }

    static String urlEncode(String filename) {
        try {
            return URLEncoder.encode(filename, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(
                    "UTF-8 charset not available (\"this should never happen\")",
                    e);
        }
    }

    public static ResourceReference create(Resource resource,
            ClientConnector connector, String key) {
        if (resource == null) {
            return null;
        } else {
            return new ResourceReference(resource, connector, key);
        }
    }

    public static Resource getResource(URLReference reference) {
        if (reference == null) {
            return null;
        }
        assert reference instanceof ResourceReference;
        return ((ResourceReference) reference).getResource();
    }
}