summaryrefslogtreecommitdiffstats
path: root/server/src/com/vaadin/terminal/ClassResource.java
blob: f63111ef9b75f5f7c2763482e841634c2a5f3836 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* 
@VaadinApache2LicenseForJavaFiles@
 */

package com.vaadin.terminal;

import java.io.Serializable;

import com.vaadin.Application;
import com.vaadin.service.FileTypeResolver;

/**
 * <code>ClassResource</code> is a named resource accessed with the class
 * loader.
 * 
 * This can be used to access resources such as icons, files, etc.
 * 
 * @see java.lang.Class#getResource(java.lang.String)
 * 
 * @author Vaadin Ltd.
 * @since 3.0
 */
@SuppressWarnings("serial")
public class ClassResource implements ApplicationResource, Serializable {

    /**
     * Default buffer size for this stream resource.
     */
    private int bufferSize = 0;

    /**
     * Default cache time for this stream resource.
     */
    private long cacheTime = DEFAULT_CACHETIME;

    /**
     * Associated class used for indetifying the source of the resource.
     */
    private final Class<?> associatedClass;

    /**
     * Name of the resource is relative to the associated class.
     */
    private final String resourceName;

    /**
     * Application used for serving the class.
     */
    private final Application application;

    /**
     * Creates a new application resource instance. The resource id is relative
     * to the location of the application class.
     * 
     * @param resourceName
     *            the Unique identifier of the resource within the application.
     * @param application
     *            the application this resource will be added to.
     */
    public ClassResource(String resourceName, Application application) {
        this(application.getClass(), resourceName, application);
    }

    /**
     * Creates a new application resource instance.
     * 
     * @param associatedClass
     *            the class of the which the resource is associated.
     * @param resourceName
     *            the Unique identifier of the resource within the application.
     * @param application
     *            the application this resource will be added to.
     */
    public ClassResource(Class<?> associatedClass, String resourceName,
            Application application) {
        this.associatedClass = associatedClass;
        this.resourceName = resourceName;
        this.application = application;
        if (resourceName == null || associatedClass == null) {
            throw new NullPointerException();
        }
        application.addResource(this);
    }

    /**
     * Gets the MIME type of this resource.
     * 
     * @see com.vaadin.terminal.Resource#getMIMEType()
     */
    @Override
    public String getMIMEType() {
        return FileTypeResolver.getMIMEType(resourceName);
    }

    /**
     * Gets the application of this resource.
     * 
     * @see com.vaadin.terminal.ApplicationResource#getApplication()
     */
    @Override
    public Application getApplication() {
        return application;
    }

    /**
     * Gets the virtual filename for this resource.
     * 
     * @return the file name associated to this resource.
     * @see com.vaadin.terminal.ApplicationResource#getFilename()
     */
    @Override
    public String getFilename() {
        int index = 0;
        int next = 0;
        while ((next = resourceName.indexOf('/', index)) > 0
                && next + 1 < resourceName.length()) {
            index = next + 1;
        }
        return resourceName.substring(index);
    }

    /**
     * Gets resource as stream.
     * 
     * @see com.vaadin.terminal.ApplicationResource#getStream()
     */
    @Override
    public DownloadStream getStream() {
        final DownloadStream ds = new DownloadStream(
                associatedClass.getResourceAsStream(resourceName),
                getMIMEType(), getFilename());
        ds.setBufferSize(getBufferSize());
        ds.setCacheTime(cacheTime);
        return ds;
    }

    /* documented in superclass */
    @Override
    public int getBufferSize() {
        return bufferSize;
    }

    /**
     * Sets the size of the download buffer used for this resource.
     * 
     * @param bufferSize
     *            the size of the buffer in bytes.
     */
    public void setBufferSize(int bufferSize) {
        this.bufferSize = bufferSize;
    }

    /* documented in superclass */
    @Override
    public long getCacheTime() {
        return cacheTime;
    }

    /**
     * Sets the length of cache expiration time.
     * 
     * <p>
     * This gives the adapter the possibility cache streams sent to the client.
     * The caching may be made in adapter or at the client if the client
     * supports caching. Zero or negavive value disbales the caching of this
     * stream.
     * </p>
     * 
     * @param cacheTime
     *            the cache time in milliseconds.
     * 
     */
    public void setCacheTime(long cacheTime) {
        this.cacheTime = cacheTime;
    }
}