001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Stephane Lacoin
018 */
019package org.nuxeo.runtime.model.persistence.fs;
020
021import static java.nio.charset.StandardCharsets.UTF_8;
022
023import java.io.IOException;
024import java.io.InputStream;
025import java.net.URL;
026
027import org.apache.commons.io.IOUtils;
028import org.nuxeo.runtime.model.persistence.AbstractContribution;
029
030public class ContributionLocation extends AbstractContribution {
031
032    protected final URL location;
033
034    public ContributionLocation(String name, URL location) {
035        super(name);
036        this.location = location;
037    }
038
039    @Override
040    public InputStream getStream() {
041        try {
042            return location.openStream();
043        } catch (IOException e) {
044            throw new RuntimeException("Cannot get '".concat(name).concat("' content"), e);
045        }
046    }
047
048    @Override
049    public String getContent() {
050        try {
051            return IOUtils.toString(location.openStream(), UTF_8);
052        } catch (IOException e) {
053            throw new RuntimeException("Cannot get '".concat(name).concat("' content"), e);
054        }
055    }
056
057    @Override
058    public URL asURL() {
059        return location;
060    }
061
062}