All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.speedment.common.rest.RestImpl Maven / Gradle / Ivy

Go to download

A Speedment bundle that shades all dependencies into one jar. This is useful when deploying an application on a server.

There is a newer version: 3.1.6
Show newest version
/**
 *
 * Copyright (c) 2006-2016, Speedment, Inc. All Rights Reserved.
 *
 * 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 com.speedment.common.rest;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Iterator;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Stream;

import static com.speedment.common.rest.Option.Type.HEADER;
import static com.speedment.common.rest.Option.Type.PARAM;
import static com.speedment.common.rest.Rest.encode;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.joining;

/**
 * Default implementation of the {@link Rest}-interface.
 * 
 * @author  Emil Forslund
 * @since   1.0.0
 */
class RestImpl implements Rest {
    
    private final Protocol protocol;
    private final String host;
    private final int port;
    private final String username;
    private final String password;

    RestImpl(Protocol protocol, String host, int port, String username, String password) {
        this.protocol = requireNonNull(protocol);
        this.host     = requireNonNull(host);
        this.port     = port;
        this.username = username; // Can be null
        this.password = password; // Can be null
    }
    
    @Override
    public CompletableFuture get(String path, Option... option) {
        return send(Method.GET, path, option);
    }

    @Override
    public CompletableFuture post(String path, Option... option) {
        return send(Method.POST, path, option);
    }

    @Override
    public CompletableFuture delete(String path, Option... option) {
        return send(Method.DELETE, path, option);
    }

    @Override
    public CompletableFuture put(String path, Option... option) {
        return send(Method.PUT, path, option);
    }

    @Override
    public CompletableFuture options(String path, Option... option) {
        return send(Method.OPTIONS, path, option);
    }
    
    @Override
    public CompletableFuture get(String path, Iterator uploader, Option... option) {
        return send(Method.GET, path, option, uploader);
    }

    @Override
    public CompletableFuture post(String path, Iterator uploader, Option... option) {
        return send(Method.POST, path, option, uploader);
    }

    @Override
    public CompletableFuture delete(String path, Iterator uploader, Option... option) {
        return send(Method.DELETE, path, option, uploader);
    }

    @Override
    public CompletableFuture put(String path, Iterator uploader, Option... option) {
        return send(Method.PUT, path, option, uploader);
    }

    @Override
    public CompletableFuture options(String path, Iterator uploader, Option... option) {
        return send(Method.OPTIONS, path, option, uploader);
    }
    
    @Override
    public CompletableFuture get(String path, InputStream body, Option... option) {
        return send(Method.GET, path, option, stream(body));
    }

    @Override
    public CompletableFuture post(String path, InputStream body, Option... option) {
        return send(Method.POST, path, option, stream(body));
    }

    @Override
    public CompletableFuture delete(String path, InputStream body, Option... option) {
        return send(Method.DELETE, path, option, stream(body));
    }

    @Override
    public CompletableFuture put(String path, InputStream body, Option... option) {
        return send(Method.PUT, path, option, stream(body));
    }

    @Override
    public CompletableFuture options(String path, InputStream body, Option... option) {
        return send(Method.OPTIONS, path, option, stream(body));
    }
    
    @Override
    public CompletableFuture get(String path, String data, Option... option) {
        return send(Method.GET, path, option, new SingletonIterator<>(data));
    }

    @Override
    public CompletableFuture post(String path, String data, Option... option) {
        return send(Method.POST, path, option, new SingletonIterator<>(data));
    }

    @Override
    public CompletableFuture delete(String path, String data, Option... option) {
        return send(Method.DELETE, path, option, new SingletonIterator<>(data));
    }

    @Override
    public CompletableFuture put(String path, String data, Option... option) {
        return send(Method.PUT, path, option, new SingletonIterator<>(data));
    }

    @Override
    public CompletableFuture options(String path, String data, Option... option) {
        return send(Method.OPTIONS, path, option, new SingletonIterator<>(data));
    }
    
    protected String getProtocol() {
        switch (protocol) {
            case HTTP : return "http";
            case HTTPS : return "https";
            default : throw new UnsupportedOperationException(
                "Unknown enum constant '" + protocol + "'."
            );
        }
    }

    protected String getHost() {
        return host;
    }

    protected int getPort() {
        return port;
    }
    
    protected final URL getUrl(String path, Param... param) {
        try {
            final StringBuilder url = new StringBuilder()
                .append(getProtocol())
                .append("://")
                .append(host);
            
            if (port > 0) {
                url.append(":").append(port);
            }
            
            url.append("/").append(path);
            
            if (param.length > 0) {
                url.append(
                    Stream.of(param)
                        .map(p -> 
                            encode(p.getKey()) + 
                            "=" + 
                            encode(p.getValue())
                        )
                        .collect(joining("&", "?", ""))
                );
            }
            
            return new URL(url.toString());
        } catch (final MalformedURLException ex) {
            throw new RuntimeException("Error building URL.", ex);
        }
    }
    
    private final static int BUFFER_SIZE = 1024;
    private StreamConsumer stream(InputStream in) {
        return out -> {
            final byte[] buffer = new byte[BUFFER_SIZE];
            int len;
            while ((len = in.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
        };
    }
    
    private CompletableFuture send(Method method, String path, Option[] options) {
        return send(method, path, options, NO_ITERATOR);
    }
    
    private CompletableFuture send(Method method, String path, Option[] options, Iterator iterator) {
        if (iterator == NO_ITERATOR) {
            return send(method, path, options, StreamConsumer.IGNORE);
        } else {
            return send(method, path, options, out -> {
                while (iterator.hasNext()) {
                    out.write(iterator.next().getBytes(StandardCharsets.UTF_8));
                }
            });
        }
    }
    
    private CompletableFuture send(Method method, String path, Option[] options, StreamConsumer outStreamConsumer) {
        return CompletableFuture.supplyAsync(() -> {
            final Param[] params = Stream.of(options).filter(o -> o.getType() == PARAM).toArray(Param[]::new);
            final Header[] headers = Stream.of(options).filter(o -> o.getType() == HEADER).toArray(Header[]::new);
            
            final URL url = getUrl(path, params);
            
            HttpURLConnection conn = null;
            try {
                conn = (HttpURLConnection) url.openConnection();
                
                switch (method) {
                    case POST    : conn.setRequestMethod("POST"); break;
                    case GET     : conn.setRequestMethod("GET"); break;
                    case DELETE  : conn.setRequestMethod("DELETE"); break;
                    case OPTIONS : conn.setRequestMethod("OPTIONS"); break;
                    case PUT     : conn.setRequestMethod("PUT"); break;
                    default : throw new UnsupportedOperationException(
                        "Unknown enum constant '" + method + "'."
                    );
                }
                
                if (username != null && password != null) {
                    final byte[] authentication = (username + ":" + password).getBytes();
                    final String encoding = Base64.getEncoder().encodeToString(authentication);
                    conn.setRequestProperty("Authorization", "Basic " + encoding);
                }
                
                for (final Header header : headers) {
                    conn.setRequestProperty(
                        header.getKey(), 
                        header.getValue());
                }
                
                conn.setUseCaches(false);
                conn.setAllowUserInteraction(false);
                
                final boolean doOutput = outStreamConsumer != StreamConsumer.IGNORE;
                conn.setDoOutput(doOutput);
                
                conn.connect();
                if (doOutput) {
                    try (final OutputStream out = conn.getOutputStream()) {
                        outStreamConsumer.writeTo(out);
                    }
                }

                int status = getResponseCodeFrom(conn);
                final String text;
                
                try (final BufferedReader rd = new BufferedReader(
                        new InputStreamReader(status >= 400
                            ? conn.getErrorStream()
                            : conn.getInputStream()))) {
                    
                    final StringBuilder sb = new StringBuilder();
                    String line;
                    while ((line = rd.readLine()) != null) {
                        sb.append(line);
                    }
                    
                    text = sb.toString();
                }
                
                return new Response(status, text, conn.getHeaderFields());
            } catch (final IOException ex) {
                throw new RuntimeException("Could not send " + method.name() + "-command.", ex);
            } finally {
                if (conn != null) {
                    conn.disconnect();
                }
            }
        });
    }
    
    private static int getResponseCodeFrom(HttpURLConnection conn) throws IOException {
        try {
            return conn.getResponseCode();
        } catch (final FileNotFoundException ex) {
            return 404;
        }
    }
    
    private final static Iterator NO_ITERATOR = new Iterator() {
        @Override
        public boolean hasNext() {
            return false;
        }

        @Override
        public String next() {
            throw new IllegalStateException(
                "This method should never be called."
            );
        }
    };
    
    private final static class SingletonIterator implements Iterator {
        
        private final AtomicBoolean first = new AtomicBoolean(true);
        private final T instance;
        
        private SingletonIterator(T instance) {
            this.instance = instance;
        }

        @Override
        public boolean hasNext() {
            return first.compareAndSet(true, false);
        }

        @Override
        public T next() {
            return instance;
        }
    }
    
    @FunctionalInterface
    private interface StreamConsumer {
        StreamConsumer IGNORE = o -> {};
        void writeTo(OutputStream out) throws IOException;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy