com.envimate.httpmate.spark.SparkRouteWebserviceAdapter Maven / Gradle / Ivy
/*
* Copyright (c) 2018 envimate GmbH - https://envimate.com/.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.envimate.httpmate.spark;
import com.envimate.httpmate.HttpMate;
import com.envimate.httpmate.request.RawHttpRequest;
import com.envimate.httpmate.response.HttpResponse;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import spark.Request;
import spark.Response;
import spark.Route;
import spark.utils.IOUtils;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import static com.envimate.httpmate.request.RawHttpRequest.rawHttpRequest;
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.toMap;
import static java.util.stream.Collectors.toSet;
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
final class SparkRouteWebserviceAdapter implements Route {
private final HttpMate httpMate;
@Override
public Object handle(final Request request, final Response response) {
final String httpRequestMethod = request.requestMethod();
final String path = request.pathInfo();
final Map headers = request.headers().stream()
.collect(toMap(key -> key, request::headers));
final Map queryParameters = extractQueryParameters(request);
final InputStream body;
try {
body = request.raw().getInputStream();
} catch (final IOException e) {
throw new RuntimeException(e);
}
final RawHttpRequest rawHttpRequest = rawHttpRequest(httpRequestMethod, path, queryParameters, headers, body);
final HttpResponse webServiceResponse = this.httpMate.handleRequest(rawHttpRequest);
response.status(webServiceResponse.status());
webServiceResponse.headers().forEach(response::header);
streamToResponse(webServiceResponse, response);
return null;
}
private static Map extractQueryParameters(final Request request) {
final Set queryParametersSet = request.queryParams();
final Map queryParameters = new HashMap<>();
for (final String parameter : queryParametersSet) {
final String[] values = request.queryParamsValues(parameter);
final Set valueSet = stream(values).collect(toSet());
if (valueSet.isEmpty()) {
queryParameters.put(parameter, null);
} else {
queryParameters.put(parameter, valueSet.iterator().next());
}
}
return queryParameters;
}
private static void streamToResponse(final HttpResponse webServiceResponse, final Response response) {
try (InputStream inputStream = webServiceResponse.body();
OutputStream outputStream = response.raw().getOutputStream()) {
IOUtils.copy(inputStream, outputStream);
} catch (IOException e) {
throw new RuntimeException("Something is irrepairably wrong.");
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy