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.test;
020
021import java.io.ByteArrayInputStream;
022import java.io.IOException;
023import java.io.InputStream;
024import java.net.URL;
025
026import org.nuxeo.runtime.model.StreamRef;
027import org.nuxeo.runtime.test.protocols.inline.InlineURLFactory;
028
029/**
030 * InlineRef allows to create stream ref on the fly, using only a String.
031 *
032 * @since 5.8
033 */
034public class InlineRef implements StreamRef {
035
036    protected final String id;
037
038    protected final String content;
039
040    public InlineRef(String id, String content) {
041        this.id = id;
042        this.content = content;
043    }
044
045    @Override
046    public String getId() {
047        return id;
048    }
049
050    @Override
051    public InputStream getStream() throws IOException {
052        return new ByteArrayInputStream(content.getBytes());
053    }
054
055    @Override
056    public URL asURL() {
057        try {
058            return InlineURLFactory.newURL(content);
059        } catch (IOException e) {
060            throw new RuntimeException("Cannot encode inline:... URL", e);
061        }
062    }
063
064}