/*
 * Copyright 2016-2017 52°North Initiative for Geospatial Open Source
 * Software GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.n52.javaps.engine.impl;

import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;

import org.n52.shetland.util.HTTP;
import org.n52.shetland.ogc.ows.OwsCode;
import org.n52.shetland.ogc.wps.Format;
import org.n52.shetland.ogc.wps.data.Body;
import org.n52.shetland.ogc.wps.data.ReferenceProcessData;
import org.n52.shetland.ogc.wps.data.ValueProcessData;
import org.n52.shetland.ogc.wps.data.impl.InMemoryValueProcessData;


/**
 * TODO JavaDoc
 *
 * @author Christian Autermann
 */
public class ResolvableReferenceProcessData extends ReferenceProcessData {


    public ResolvableReferenceProcessData(OwsCode id) {
        this(id, null, null, null);
    }

    public ResolvableReferenceProcessData(OwsCode id, URI uri) {
        this(id, null, uri, null);
    }

    public ResolvableReferenceProcessData(OwsCode id, Format format, URI uri) {
        this(id, format, uri, null);
    }

    public ResolvableReferenceProcessData(OwsCode id, Format format, URI uri, Body body) {
        super(id, format, uri, body);
    }

    public ResolvableReferenceProcessData() {
        this(null, null, null, null);
    }

    public ResolvableReferenceProcessData(ReferenceProcessData data) {
        this(data.getId(), data.getFormat(), data.getURI(), data.getBody().orElse(null));
    }

    public ValueProcessData resolve()
            throws IOException {
        //TODO save as a file?
        final byte[] bytes;
        if (!getBody().isPresent()) {
            bytes = HTTP.get(getURI());
        } else {
            bytes = HTTP.post(getURI(), getBody().get().getBody()
                              .getBytes(StandardCharsets.UTF_8));
        }
        return new InMemoryValueProcessData(getId(), getFormat(), bytes);
    }

}
@@ -700,40 +700,121 @@
 .io.
-IOException;%0Aimport java.net.URI
+FileInputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.net.URI;%0Aimport java.net.URL
 ;%0Aim
@@ -858,43 +858,8 @@
 s;%0A%0A
-import org.n52.shetland.util.HTTP;%0A
 impo
@@ -1160,16 +1160,93 @@
 ssData;%0A
+import org.n52.shetland.util.HTTP;%0A%0Aimport com.google.common.io.ByteStreams;%0A
 %0A%0A/**%0A *
@@ -2263,24 +2263,185 @@
 resent()) %7B%0A
+            switch (getURI().getScheme()) %7B%0A            case %22file%22:%0A                bytes = getFromLocalDisc();%0A                break;%0A            default:%0A    
             
@@ -2464,24 +2464,38 @@
 (getURI());%0A
+            %7D%0A
         %7D el
@@ -2722,9 +2722,225 @@
 %0A    %7D%0A%0A
-%7D
+    private byte%5B%5D getFromLocalDisc() throws IOException %7B%0A        URL fileURL = getURI().toURL();%0A        InputStream is = new FileInputStream(fileURL.getFile());%0A        return ByteStreams.toByteArray(is);%0A    %7D%0A%0A%7D%0A

allow references from local disc