3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.api.utils;
22 import com.google.common.base.Preconditions;
23 import com.google.common.base.Throwables;
24 import com.google.common.io.Files;
26 import java.io.IOException;
28 import java.nio.charset.Charset;
29 import java.util.Arrays;
30 import java.util.HashMap;
31 import java.util.Locale;
33 import java.util.stream.Stream;
34 import org.sonar.api.batch.ScannerSide;
35 import org.sonar.api.ce.ComputeEngineSide;
36 import org.sonar.api.server.ServerSide;
39 * Reads different types of URI. Supported schemes are http and file.
46 public class UriReader {
48 private final Map<String, SchemeProcessor> processorsByScheme = new HashMap<>();
50 public UriReader(SchemeProcessor[] processors) {
51 Stream.concat(Stream.of(new FileProcessor()), Arrays.stream(processors)).forEach(processor -> {
52 for (String scheme : processor.getSupportedSchemes()) {
53 processorsByScheme.put(scheme.toLowerCase(Locale.ENGLISH), processor);
59 * Reads all bytes from uri. It throws an unchecked exception if an error occurs.
61 public byte[] readBytes(URI uri) {
62 return searchForSupportedProcessor(uri).readBytes(uri);
66 * Reads all characters from uri, using the given character set.
67 * It throws an unchecked exception if an error occurs.
69 public String readString(URI uri, Charset charset) {
70 return searchForSupportedProcessor(uri).readString(uri, charset);
74 * Returns a detailed description of the given uri. For example HTTP URIs are completed
75 * with the configured HTTP proxy.
77 public String description(URI uri) {
78 SchemeProcessor reader = searchForSupportedProcessor(uri);
80 return reader.description(uri);
83 SchemeProcessor searchForSupportedProcessor(URI uri) {
84 SchemeProcessor processor = processorsByScheme.get(uri.getScheme().toLowerCase(Locale.ENGLISH));
85 Preconditions.checkArgument(processor != null, "URI schema is not supported: " + uri.getScheme());
89 public abstract static class SchemeProcessor {
90 protected abstract String[] getSupportedSchemes();
92 protected abstract byte[] readBytes(URI uri);
94 protected abstract String readString(URI uri, Charset charset);
96 protected abstract String description(URI uri);
100 * This implementation is not exposed in API and is kept private.
102 private static class FileProcessor extends SchemeProcessor {
105 public String[] getSupportedSchemes() {
106 return new String[]{"file"};
110 protected byte[] readBytes(URI uri) {
112 return Files.toByteArray(new File(uri));
113 } catch (IOException e) {
114 throw Throwables.propagate(e);
119 protected String readString(URI uri, Charset charset) {
121 return Files.toString(new File(uri), charset);
122 } catch (IOException e) {
123 throw Throwables.propagate(e);
128 protected String description(URI uri) {
129 return new File(uri).getAbsolutePath();