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.protocols.inline;
020
021import java.io.ByteArrayInputStream;
022import java.io.ByteArrayOutputStream;
023import java.io.IOException;
024import java.io.InputStream;
025import java.io.ObjectInputStream;
026import java.io.ObjectOutputStream;
027import java.net.URL;
028import java.net.URLStreamHandlerFactory;
029import java.util.regex.Matcher;
030import java.util.regex.Pattern;
031
032import org.nuxeo.common.utils.Base64;
033import org.nuxeo.common.utils.URLStreamHandlerFactoryInstaller;
034
035public class InlineURLFactory {
036
037    public static void install() {
038        shf = new InlineURLStreamHandlerFactory();
039        try {
040            URLStreamHandlerFactoryInstaller.installURLStreamHandlerFactory(shf);
041        } catch (Exception e) {
042            throw new RuntimeException("Cannot install inline URLs", e);
043        }
044    }
045
046    protected static URLStreamHandlerFactory shf;
047
048    public static void uninstall() {
049        try {
050            URLStreamHandlerFactoryInstaller.uninstallURLStreamHandlerFactory(shf);
051        } catch (Exception cause) {
052            throw new RuntimeException("Cannot uninstall inline URLs", cause);
053        } finally {
054            shf = null;
055        }
056
057    }
058
059    public static <T> byte[] marshall(T content) throws IOException {
060        ByteArrayOutputStream bos = new ByteArrayOutputStream();
061        ObjectOutputStream oos = new ObjectOutputStream(bos);
062        oos.writeObject(content);
063        return bos.toByteArray();
064    }
065
066    public static <T> T unmarshall(Class<T> clazz, byte[] data) throws IOException {
067        InputStream is = new ByteArrayInputStream(data);
068        ObjectInputStream ois = new ObjectInputStream(is);
069        try {
070            return clazz.cast(ois.readObject());
071        } catch (ClassNotFoundException e) {
072            throw new RuntimeException("Cannot decode, object is not of class " + clazz.getSimpleName(), e);
073        }
074    }
075
076    public static <T> URL newURL(T content) throws IOException {
077        byte[] data = marshall(content);
078        return newURL("application/java", data);
079    }
080
081    public static URL newURL(String mimetype, byte[] data) throws IOException {
082        return new URL("inline:".concat(mimetype).concat(";base64,".concat(Base64.encodeBytes(data))));
083    }
084
085    public static <T> T newObject(Class<T> clazz, URL url) throws IOException {
086        byte[] data = getBytes(url);
087        return unmarshall(clazz, data);
088    }
089
090    protected static final Pattern pattern = Pattern.compile("inline:(.*);base64,(.*)");
091
092    public static byte[] getBytes(URL url) throws IOException {
093        Matcher matcher = pattern.matcher(url.toExternalForm());
094        if (!matcher.matches()) {
095            throw new IllegalArgumentException("'" + url + "' should be 'inline:mimetype;base64,content'");
096        }
097        @SuppressWarnings("unused")
098        String mimetype = matcher.group(1);
099        String data = matcher.group(2);
100        return Base64.decode(data);
101    }
102
103}