aboutsummaryrefslogtreecommitdiffstats
path: root/netx/net/sourceforge/jnlp/services/XPersistenceService.java
blob: be98d127506c1d16a748f98d34093394184b32ab (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
177
178
179
// Copyright (C) 2001-2003 Jon A. Maxwell (JAM)
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

package net.sourceforge.jnlp.services;

import java.io.*;
import java.net.*;
import java.util.*;
import javax.jnlp.*;

import net.sourceforge.jnlp.cache.*;
import net.sourceforge.jnlp.config.DeploymentConfiguration;
import net.sourceforge.jnlp.runtime.*;
import net.sourceforge.jnlp.util.FileUtils;

/**
 * The BasicService JNLP service.
 *
 * @author <a href="mailto:jmaxwell@users.sourceforge.net">Jon A. Maxwell (JAM)</a> - initial author
 * @version $Revision: 1.7 $
 */
class XPersistenceService implements PersistenceService {

    // todo: recheck delete, etc to make sure security is tight

    protected XPersistenceService() {
    }

    /**
     * Checks whether the application has access to URL area
     * requested.  If the method returns normally then the specified
     * location can be accessed by the current application.
     *
     * @throws MalformedURLException if the application cannot access the location
     */
    protected void checkLocation(URL location) throws MalformedURLException {
        ApplicationInstance app = JNLPRuntime.getApplication();
        if (app == null)
            throw new MalformedURLException("Cannot determine the current application.");

        URL source = app.getJNLPFile().getCodeBase();

        if (!source.getHost().equalsIgnoreCase(location.getHost()))
            throw new MalformedURLException("Cannot access data from a different host.");

        // test for above codebase, not perfect but works for now

        String requestPath = location.getFile();
        if (-1 != requestPath.lastIndexOf("/"))
            requestPath = requestPath.substring(0, requestPath.lastIndexOf("/"));
        else
            requestPath = "";

        if (JNLPRuntime.isDebug()) {
            System.out.println("codebase path: " + source.getFile());
            System.out.println("request path: " + requestPath);
        }

        if (!source.getFile().startsWith(requestPath))
            throw new MalformedURLException("Cannot access data below source URL path.");
    }

    /**
     * Converts a URL into a file in the persistence store.
     *
     * @return the file
     */
    protected File toCacheFile(URL location) throws MalformedURLException {
        String pcache = JNLPRuntime.getConfiguration()
                .getProperty(DeploymentConfiguration.KEY_USER_PERSISTENCE_CACHE_DIR);
        return CacheUtil.urlToPath(location, pcache);
    }

    /**
     *
     * @return the maximum size of storage that got granted, in bytes
     * @throws MalformedURLException if the application cannot access the location
     */
    public long create(URL location, long maxsize) throws MalformedURLException, IOException {
        checkLocation(location);

        File file = toCacheFile(location);
        file.getParentFile().mkdirs();

        if (file.exists())
            throw new IOException("File already exists.");

        FileUtils.createRestrictedFile(file, true);

        return maxsize;
    }

    /**
     *
     * @throws MalformedURLException if the application cannot access the location
     */
    public void delete(URL location) throws MalformedURLException, IOException {
        checkLocation(location);

        toCacheFile(location).delete();
    }

    /**
     *
     * @throws MalformedURLException if the application cannot access the location
     */
    public FileContents get(URL location) throws MalformedURLException, IOException, FileNotFoundException {
        checkLocation(location);

        File file = toCacheFile(location);
        if (!file.exists())
            throw new FileNotFoundException("Persistence store for "
                    + location.toString() + " is not found.");
        file.getParentFile().mkdirs();

        return (FileContents) ServiceUtil.createPrivilegedProxy(FileContents.class, new XFileContents(file));
    }

    /**
     *
     * @throws MalformedURLException if the application cannot access the location
     */
    public String[] getNames(URL location) throws MalformedURLException, IOException {
        checkLocation(location);

        File file = toCacheFile(location);
        if (!file.isDirectory())
            return new String[0];

        List<String> result = new ArrayList<String>();

        // check whether this is right: only add files and not directories.
        File entries[] = file.listFiles();
        for (int i = 0; i < entries.length; i++)
            if (entries[i].isFile())
                result.add(entries[i].getName());

        return result.toArray(new String[result.size()]);
    }

    /**
     *
     * @throws MalformedURLException if the application cannot access the location
     */
    public int getTag(URL location) throws MalformedURLException, IOException {
        checkLocation(location);

        // todo: actually implement tags

        if (toCacheFile(location).exists())
            return PersistenceService.CACHED;

        return PersistenceService.CACHED;
    }

    /**
     *
     * @throws MalformedURLException if the application cannot access the location
     */
    public void setTag(URL location, int tag) throws MalformedURLException, IOException {
        checkLocation(location);

        // todo: actually implement tags
    }

}