2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
package org.apache.ivy.plugins.repository.url;
21
import java.io.IOException;
23
import java.net.URISyntaxException;
25
import java.util.ArrayList;
26
import java.util.Collections;
27
import java.util.HashMap;
28
import java.util.List;
29
import java.util.ListIterator;
32
import org.apache.ivy.plugins.repository.AbstractRepository;
33
import org.apache.ivy.plugins.repository.RepositoryCopyProgressListener;
34
import org.apache.ivy.plugins.repository.Resource;
35
import org.apache.ivy.plugins.repository.TransferEvent;
36
import org.apache.ivy.util.FileUtil;
37
import org.apache.ivy.util.url.ApacheURLLister;
39
public class URLRepository extends AbstractRepository {
40
private RepositoryCopyProgressListener progress = new RepositoryCopyProgressListener(this);
42
private Map resourcesCache = new HashMap();
44
public Resource getResource(String source) throws IOException {
45
Resource res = (Resource) resourcesCache.get(source);
47
res = new URLResource(new URL(source));
48
resourcesCache.put(source, res);
53
public void get(String source, File destination) throws IOException {
54
fireTransferInitiated(getResource(source), TransferEvent.REQUEST_GET);
56
Resource res = getResource(source);
57
long totalLength = res.getContentLength();
58
if (totalLength > 0) {
59
progress.setTotalLength(new Long(totalLength));
61
FileUtil.copy(new URL(source), destination, progress);
62
} catch (IOException ex) {
63
fireTransferError(ex);
65
} catch (RuntimeException ex) {
66
fireTransferError(ex);
69
progress.setTotalLength(null);
73
public void put(File source, String destination, boolean overwrite) throws IOException {
75
throw new UnsupportedOperationException(
76
"URL repository do not support append operations at the moment");
79
fireTransferInitiated(getResource(destination), TransferEvent.REQUEST_PUT);
81
long totalLength = source.length();
82
if (totalLength > 0) {
83
progress.setTotalLength(new Long(totalLength));
85
FileUtil.copy(source, new URL(destination), progress);
86
} catch (IOException ex) {
87
fireTransferError(ex);
89
} catch (RuntimeException ex) {
90
fireTransferError(ex);
93
progress.setTotalLength(null);
97
private ApacheURLLister lister = new ApacheURLLister();
99
public List list(String parent) throws IOException {
100
if (parent.startsWith("http")) {
101
List urls = lister.listAll(new URL(parent));
103
List ret = new ArrayList(urls.size());
104
for (ListIterator iter = urls.listIterator(); iter.hasNext();) {
105
URL url = (URL) iter.next();
106
ret.add(url.toExternalForm());
110
} else if (parent.startsWith("file")) {
113
path = new URI(parent).getPath();
114
} catch (URISyntaxException e) {
115
IOException ioe = new IOException("Couldn't list content of '" + parent + "'");
120
File file = new File(path);
121
if (file.exists() && file.isDirectory()) {
122
String[] files = file.list();
123
List ret = new ArrayList(files.length);
124
URL context = path.endsWith("/") ? new URL(parent) : new URL(parent + "/");
125
for (int i = 0; i < files.length; i++) {
126
ret.add(new URL(context, files[i]).toExternalForm());
130
return Collections.EMPTY_LIST;