Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove MediaType from public methods in API module #232

Merged
merged 4 commits into from
Aug 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import com.google.common.base.Objects;
import com.google.common.collect.ImmutableSet;
import com.google.common.net.MediaType;
import com.hotels.styx.api.cookies.ResponseCookie;
import com.hotels.styx.api.messages.HttpResponseStatus;
import com.hotels.styx.api.messages.HttpVersion;
Expand All @@ -33,7 +32,6 @@
import static com.google.common.base.Objects.toStringHelper;
import static com.google.common.base.Preconditions.checkArgument;
import static com.hotels.styx.api.HttpHeaderNames.CONTENT_LENGTH;
import static com.hotels.styx.api.HttpHeaderNames.CONTENT_TYPE;
import static com.hotels.styx.api.HttpHeaderNames.SET_COOKIE;
import static com.hotels.styx.api.HttpHeaderNames.TRANSFER_ENCODING;
import static com.hotels.styx.api.HttpHeaderValues.CHUNKED;
Expand Down Expand Up @@ -458,10 +456,6 @@ public Builder addHeader(CharSequence name, Object value) {
return this;
}

public Builder contentType(MediaType mediaType) {
return addHeader(CONTENT_TYPE, mediaType);
}

/**
* Removes the header with the specified name.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import com.google.common.base.Objects;
import com.google.common.collect.ImmutableSet;
import com.google.common.net.MediaType;
import com.hotels.styx.api.cookies.ResponseCookie;
import com.hotels.styx.api.messages.HttpResponseStatus;
import com.hotels.styx.api.messages.HttpVersion;
Expand All @@ -37,7 +36,6 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.hotels.styx.api.FlowControlDisableOperator.disableFlowControl;
import static com.hotels.styx.api.HttpHeaderNames.CONTENT_LENGTH;
import static com.hotels.styx.api.HttpHeaderNames.CONTENT_TYPE;
import static com.hotels.styx.api.HttpHeaderNames.SET_COOKIE;
import static com.hotels.styx.api.HttpHeaderNames.TRANSFER_ENCODING;
import static com.hotels.styx.api.HttpHeaderValues.CHUNKED;
Expand Down Expand Up @@ -445,10 +443,6 @@ public Builder addHeader(CharSequence name, Object value) {
return this;
}

public Builder contentType(MediaType mediaType) {
return addHeader(CONTENT_TYPE, mediaType);
}

/**
* Removes the header with the specified name.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,19 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.hotels.styx.api.FullHttpResponse;
import com.hotels.styx.api.HttpRequest;
import com.hotels.styx.api.HttpResponse;
import com.hotels.styx.common.http.handler.BaseHttpHandler;
import com.hotels.styx.api.messages.HttpResponseStatus;
import com.hotels.styx.common.http.handler.BaseHttpHandler;

import java.util.Map;
import java.util.SortedMap;
import java.util.concurrent.ExecutorService;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.net.MediaType.JSON_UTF_8;
import static com.hotels.styx.api.FullHttpResponse.response;
import static com.hotels.styx.api.HttpHeaderNames.CONTENT_TYPE;
import static com.hotels.styx.api.messages.HttpResponseStatus.INTERNAL_SERVER_ERROR;
import static com.hotels.styx.api.messages.HttpResponseStatus.NOT_IMPLEMENTED;
import static com.hotels.styx.api.messages.HttpResponseStatus.OK;
Expand Down Expand Up @@ -73,9 +74,9 @@ private static ObjectMapper mapper() {
@Override
protected HttpResponse doHandle(HttpRequest request) {
SortedMap<String, HealthCheck.Result> results = runHealthChecks();
return FullHttpResponse.response(responseStatus(results))
return response(responseStatus(results))
.disableCaching()
.contentType(JSON_UTF_8)
.addHeader(CONTENT_TYPE, JSON_UTF_8.toString())
.body(body(request, results), UTF_8)
.build()
.toStreamingResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
*/
package com.hotels.styx.admin.handlers;

import com.hotels.styx.api.FullHttpResponse;
import com.hotels.styx.api.HttpRequest;
import com.hotels.styx.api.HttpResponse;
import com.hotels.styx.common.http.handler.BaseHttpHandler;
import com.hotels.styx.api.HttpRequest;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.net.HttpHeaders.CONTENT_LANGUAGE;
import static com.google.common.net.MediaType.HTML_UTF_8;
import static com.hotels.styx.api.FullHttpResponse.response;
import static com.hotels.styx.api.HttpHeaderNames.CONTENT_TYPE;
import static com.hotels.styx.api.messages.HttpResponseStatus.OK;
import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;
Expand Down Expand Up @@ -56,8 +57,8 @@ private static String generateHtml(Iterable<Link> links) {

@Override
protected HttpResponse doHandle(HttpRequest request) {
return FullHttpResponse.response(OK)
.contentType(HTML_UTF_8)
return response(OK)
.addHeader(CONTENT_TYPE, HTML_UTF_8.toString())
.header(CONTENT_LANGUAGE, "en")
.body(html, UTF_8)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.hotels.styx.admin.dashboard.JsonSupplier;
import com.hotels.styx.admin.handlers.json.JsonReformatter;
import com.hotels.styx.api.Clock;
import com.hotels.styx.api.HttpRequest;
import com.hotels.styx.api.HttpResponse;
import com.hotels.styx.common.http.handler.BaseHttpHandler;
import org.slf4j.Logger;
Expand All @@ -32,11 +33,11 @@
import static com.google.common.net.MediaType.JSON_UTF_8;
import static com.hotels.styx.api.Clocks.systemClock;
import static com.hotels.styx.api.FullHttpResponse.response;
import static com.hotels.styx.api.HttpHeaderNames.CONTENT_TYPE;
import static com.hotels.styx.api.messages.HttpResponseStatus.INTERNAL_SERVER_ERROR;
import static com.hotels.styx.api.messages.HttpResponseStatus.OK;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.slf4j.LoggerFactory.getLogger;
import com.hotels.styx.api.HttpRequest;

/**
* Handler for returning JSON. If a cache expiration value is present, the JSON is not regenerated on every call, unless
Expand Down Expand Up @@ -93,7 +94,7 @@ protected HttpResponse doHandle(HttpRequest request) {

return response(OK)
.disableCaching()
.contentType(JSON_UTF_8)
.addHeader(CONTENT_TYPE, JSON_UTF_8.toString())
.body(jsonContent, UTF_8)
.build()
.toStreamingResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,21 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.hotels.styx.api.HttpRequest;
import com.hotels.styx.api.HttpResponse;
import com.hotels.styx.common.http.handler.BaseHttpHandler;
import com.hotels.styx.api.service.BackendService;
import com.hotels.styx.api.service.spi.Registry;
import com.hotels.styx.common.http.handler.BaseHttpHandler;

import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.net.MediaType.JSON_UTF_8;
import static com.hotels.styx.infrastructure.configuration.json.ObjectMappers.addStyxMixins;
import static com.hotels.styx.api.FullHttpResponse.response;
import static com.hotels.styx.api.HttpHeaderNames.CONTENT_TYPE;
import static com.hotels.styx.api.messages.HttpResponseStatus.INTERNAL_SERVER_ERROR;
import static com.hotels.styx.api.messages.HttpResponseStatus.OK;
import static com.hotels.styx.infrastructure.configuration.json.ObjectMappers.addStyxMixins;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.hotels.styx.api.HttpRequest;

/**
* Provides origins configuration in the form of JSON.
Expand All @@ -57,7 +58,7 @@ private HttpResponse jsonResponse(Object object, boolean prettyPrint) {
String jsonContent = marshal(object, prettyPrint);
return response(OK)
.disableCaching()
.contentType(JSON_UTF_8)
.addHeader(CONTENT_TYPE, JSON_UTF_8.toString())
.body(jsonContent, UTF_8)
.build()
.toStreamingResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
import com.fasterxml.jackson.databind.ObjectWriter;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import com.hotels.styx.api.FullHttpResponse;
import com.hotels.styx.api.HttpRequest;
import com.hotels.styx.api.HttpResponse;
import com.hotels.styx.api.Id;
import com.hotels.styx.api.client.OriginsChangeListener;
import com.hotels.styx.api.client.OriginsSnapshot;
import com.hotels.styx.common.http.handler.BaseHttpHandler;
import com.hotels.styx.client.origincommands.GetOriginsInventorySnapshot;
import com.hotels.styx.common.http.handler.BaseHttpHandler;
import org.slf4j.Logger;

import java.util.Map;
Expand All @@ -35,11 +35,12 @@
import static com.fasterxml.jackson.databind.SerializationFeature.FAIL_ON_EMPTY_BEANS;
import static com.google.common.net.MediaType.JSON_UTF_8;
import static com.hotels.styx.admin.support.Json.PRETTY_PRINTER;
import static com.hotels.styx.infrastructure.configuration.json.ObjectMappers.addStyxMixins;
import static com.hotels.styx.api.FullHttpResponse.response;
import static com.hotels.styx.api.HttpHeaderNames.CONTENT_TYPE;
import static com.hotels.styx.api.messages.HttpResponseStatus.OK;
import static com.hotels.styx.infrastructure.configuration.json.ObjectMappers.addStyxMixins;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.slf4j.LoggerFactory.getLogger;
import com.hotels.styx.api.HttpRequest;

/**
* Returns an origins inventory snapshot in an HTTP response.
Expand All @@ -64,8 +65,8 @@ public OriginsInventoryHandler(EventBus eventBus) {

@Override
protected HttpResponse doHandle(HttpRequest request) {
return FullHttpResponse.response(OK)
.contentType(JSON_UTF_8)
return response(OK)
.addHeader(CONTENT_TYPE, JSON_UTF_8.toString())
.disableCaching()
.body(content(isPrettyPrint(request)), UTF_8)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
*/
package com.hotels.styx.admin.handlers;

import com.hotels.styx.api.HttpRequest;
import com.hotels.styx.api.HttpResponse;
import com.hotels.styx.common.http.handler.BaseHttpHandler;

import static com.google.common.net.MediaType.PLAIN_TEXT_UTF_8;
import static com.hotels.styx.api.FullHttpResponse.response;
import static com.hotels.styx.api.HttpHeaderNames.CONTENT_TYPE;
import static com.hotels.styx.api.messages.HttpResponseStatus.OK;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.hotels.styx.api.HttpRequest;

/**
* A simple handler that can confirm that the admin interface is available. Returns a non-caching plain-text response
Expand All @@ -33,7 +34,7 @@ public class PingHandler extends BaseHttpHandler {
protected HttpResponse doHandle(HttpRequest request) {
return response(OK)
.disableCaching()
.contentType(PLAIN_TEXT_UTF_8)
.addHeader(CONTENT_TYPE, PLAIN_TEXT_UTF_8.toString())
.body("pong", UTF_8)
.build()
.toStreamingResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/
package com.hotels.styx.admin.handlers;

import com.hotels.styx.api.FullHttpResponse;
import com.hotels.styx.api.HttpHandler;
import com.hotels.styx.api.HttpInterceptor;
import com.hotels.styx.api.HttpRequest;
import com.hotels.styx.api.HttpResponse;
import com.hotels.styx.api.StyxObservable;
import com.hotels.styx.proxy.plugin.NamedPlugin;
Expand All @@ -26,13 +26,14 @@
import java.util.stream.Stream;

import static com.google.common.net.MediaType.HTML_UTF_8;
import static com.hotels.styx.api.FullHttpResponse.response;
import static com.hotels.styx.api.HttpHeaderNames.CONTENT_TYPE;
import static com.hotels.styx.api.messages.HttpResponseStatus.OK;
import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static java.util.stream.StreamSupport.stream;
import com.hotels.styx.api.HttpRequest;

/**
* Returns a simple HTML page with a list of plugins, split into enabled and disabled.
Expand All @@ -52,9 +53,9 @@ public StyxObservable<HttpResponse> handle(HttpRequest request, HttpInterceptor.
String output = section("Enabled", enabled)
+ section("Disabled", disabled);

return StyxObservable.of(FullHttpResponse.response(OK)
return StyxObservable.of(response(OK)
.body(output, UTF_8)
.contentType(HTML_UTF_8)
.addHeader(CONTENT_TYPE, HTML_UTF_8.toString())
.build()
.toStreamingResponse());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

import static com.google.common.base.Throwables.propagate;
import static com.google.common.net.MediaType.PLAIN_TEXT_UTF_8;
import static com.hotels.styx.api.HttpHeaderNames.CONTENT_TYPE;
import static com.hotels.styx.api.HttpResponse.response;
import static com.hotels.styx.api.messages.HttpResponseStatus.BAD_REQUEST;
import static com.hotels.styx.api.messages.HttpResponseStatus.INTERNAL_SERVER_ERROR;
Expand Down Expand Up @@ -172,10 +173,10 @@ private static StyxObservable<PluginEnabledState> requestedNewState(HttpRequest
.map(PluginEnabledState::fromBoolean);
}

private HttpResponse responseWith(HttpResponseStatus status, String message) {
private static HttpResponse responseWith(HttpResponseStatus status, String message) {
return FullHttpResponse.response(status)
.body(message + "\n", UTF_8)
.contentType(PLAIN_TEXT_UTF_8)
.addHeader(CONTENT_TYPE, PLAIN_TEXT_UTF_8.toString())
.disableCaching()
.build()
.toStreamingResponse();
Expand All @@ -192,7 +193,7 @@ private static boolean parseToBoolean(String string) {
}
}

private StyxObservable<HttpResponse> handleErrors(Throwable e, HttpInterceptor.Context context) {
private static StyxObservable<HttpResponse> handleErrors(Throwable e, HttpInterceptor.Context context) {
if (e instanceof PluginNotFoundException) {
return StyxObservable.of(responseWith(NOT_FOUND, e.getMessage()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.net.MediaType.PLAIN_TEXT_UTF_8;
import static com.hotels.styx.api.HttpHeaderNames.CONTENT_TYPE;
import static com.hotels.styx.api.messages.HttpResponseStatus.OK;
import static java.nio.charset.StandardCharsets.UTF_8;

Expand All @@ -47,11 +48,10 @@ public StatusHandler(HttpHandler handler) {
public StyxObservable<HttpResponse> handle(HttpRequest request, HttpInterceptor.Context context) {
return handler.handle(request, context)
.flatMap(response -> response.toFullResponse(0xffffff))
.map(response ->
response.newBuilder()
.contentType(PLAIN_TEXT_UTF_8)
.body(statusContent(response.status()), UTF_8)
.build())
.map(response -> response.newBuilder()
.addHeader(CONTENT_TYPE, PLAIN_TEXT_UTF_8.toString())
.body(statusContent(response.status()), UTF_8)
.build())
.map(FullHttpResponse::toStreamingResponse);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import java.io.InputStream;

import static com.hotels.styx.api.HttpHeaderNames.CONTENT_TYPE;
import static com.hotels.styx.api.MediaTypes.mediaTypeOf;
import static com.hotels.styx.server.handlers.MediaTypes.mediaTypeOf;
import static com.hotels.styx.api.messages.HttpResponseStatus.FORBIDDEN;
import static com.hotels.styx.api.messages.HttpResponseStatus.INTERNAL_SERVER_ERROR;
import static com.hotels.styx.api.messages.HttpResponseStatus.NOT_FOUND;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.api;
package com.hotels.styx.server.handlers;

import com.google.common.net.MediaType;

Expand All @@ -38,7 +38,7 @@
/**
* A collection of {@link MediaType}s associated with file extensions.
*/
public final class MediaTypes {
final class MediaTypes {
private MediaTypes() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

import static com.google.common.base.Throwables.propagate;
import static com.hotels.styx.api.HttpHeaderNames.CONTENT_TYPE;
import static com.hotels.styx.api.MediaTypes.mediaTypeOf;
import static com.hotels.styx.server.handlers.MediaTypes.mediaTypeOf;
import static com.hotels.styx.common.http.handler.NotFoundHandler.NOT_FOUND_HANDLER;
import static com.hotels.styx.api.messages.HttpResponseStatus.INTERNAL_SERVER_ERROR;
import static java.nio.charset.StandardCharsets.UTF_8;
Expand Down
Loading