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
|
/*
* SonarQube
* Copyright (C) 2009-2016 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* This program 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 3 of the License, or (at your option) any later version.
*
* This program 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 program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.api.utils;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.sonar.api.batch.ScannerSide;
import org.sonar.api.ce.ComputeEngineSide;
import org.sonar.api.server.ServerSide;
/**
* Reads different types of URI. Supported schemes are http and file.
*
* @since 3.2
*/
@ScannerSide
@ServerSide
@ComputeEngineSide
public class UriReader {
private final Map<String, SchemeProcessor> processorsByScheme = Maps.newHashMap();
public UriReader(SchemeProcessor[] processors) {
List<SchemeProcessor> allProcessors = Lists.asList(new FileProcessor(), processors);
for (SchemeProcessor processor : allProcessors) {
for (String scheme : processor.getSupportedSchemes()) {
processorsByScheme.put(scheme.toLowerCase(Locale.ENGLISH), processor);
}
}
}
/**
* Reads all bytes from uri. It throws an unchecked exception if an error occurs.
*/
public byte[] readBytes(URI uri) {
return searchForSupportedProcessor(uri).readBytes(uri);
}
/**
* Reads all characters from uri, using the given character set.
* It throws an unchecked exception if an error occurs.
*/
public String readString(URI uri, Charset charset) {
return searchForSupportedProcessor(uri).readString(uri, charset);
}
/**
* Returns a detailed description of the given uri. For example HTTP URIs are completed
* with the configured HTTP proxy.
*/
public String description(URI uri) {
SchemeProcessor reader = searchForSupportedProcessor(uri);
return reader.description(uri);
}
@VisibleForTesting
SchemeProcessor searchForSupportedProcessor(URI uri) {
SchemeProcessor processor = processorsByScheme.get(uri.getScheme().toLowerCase(Locale.ENGLISH));
Preconditions.checkArgument(processor != null, "URI schema is not supported: " + uri.getScheme());
return processor;
}
public abstract static class SchemeProcessor {
protected abstract String[] getSupportedSchemes();
protected abstract byte[] readBytes(URI uri);
protected abstract String readString(URI uri, Charset charset);
protected abstract String description(URI uri);
}
/**
* This implementation is not exposed in API and is kept private.
*/
private static class FileProcessor extends SchemeProcessor {
@Override
public String[] getSupportedSchemes() {
return new String[] {"file"};
}
@Override
protected byte[] readBytes(URI uri) {
try {
return Files.toByteArray(new File(uri));
} catch (IOException e) {
throw Throwables.propagate(e);
}
}
@Override
protected String readString(URI uri, Charset charset) {
try {
return Files.toString(new File(uri), charset);
} catch (IOException e) {
throw Throwables.propagate(e);
}
}
@Override
protected String description(URI uri) {
return new File(uri).getAbsolutePath();
}
}
}
|