Skip to content

Commit

Permalink
Merge pull request akka#19460 from ktoso/wip-contettype-ktoso
Browse files Browse the repository at this point in the history
=htc akka#19418 avoid LUB problem in javadsl.model.ContentType, see SI-9621
  • Loading branch information
ktoso committed Jan 14, 2016
2 parents 3010388 + 745ae25 commit 2925bea
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 91 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.http.javadsl.model

import akka.japi.Option

/**
* Represents an Http content-type. A content-type consists of a media-type and an optional charset.
*/
// Has to be defined in Scala even though it's JavaDSL because of:
// https://issues.scala-lang.org/browse/SI-9621
object ContentType {

trait Binary extends ContentType {
}

trait NonBinary extends ContentType {
def charset: HttpCharset
}

trait WithFixedCharset extends NonBinary {
}

trait WithCharset extends NonBinary {
}

}

trait ContentType {
/**
* The media-type of this content-type.
*/
def mediaType: MediaType

/**
* True if this ContentType is non-textual.
*/
def binary: Boolean

/**
* Returns the charset if this ContentType is non-binary.
*/
def getCharsetOption: Option[HttpCharset]
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ package akka.http.javadsl.model
/**
* Represents an Http media-type. A media-type consists of a main-type and a sub-type.
*
* Has to be defined in Scala even though it's JavaDSL because of: https://issues.scala-lang.org/browse/SI-9621
*/
// Has to be defined in Scala even though it's JavaDSL because of:
// https://issues.scala-lang.org/browse/SI-9621
object MediaType {

trait Binary extends MediaType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,96 @@
package akka.http.javadsl.model;

import akka.http.impl.util.Util;
import akka.http.javadsl.model.headers.Authorization;
import akka.http.javadsl.model.headers.HttpCredentials;
import akka.http.javadsl.model.headers.*;
import akka.japi.Pair;

public class JavaApiTestCases {
/** Builds a request for use on the client side */
public static HttpRequest buildRequest() {
return
HttpRequest.create()
.withMethod(HttpMethods.POST)
.withUri("/send");
}
/**
* Builds a request for use on the client side
*/
public static HttpRequest buildRequest() {
return
HttpRequest.create()
.withMethod(HttpMethods.POST)
.withUri("/send");
}

/** A simple handler for an Http server */
public static HttpResponse handleRequest(HttpRequest request) {
if (request.method() == HttpMethods.GET) {
Uri uri = request.getUri();
if (uri.path().equals("/hello")) {
String name = Util.getOrElse(uri.query().get("name"), "Mister X");

return
HttpResponse.create()
.withEntity("Hello " + name + "!");
} else
return
HttpResponse.create()
.withStatus(404)
.withEntity("Not found");
} else
return
HttpResponse.create()
/**
* A simple handler for an Http server
*/
public static HttpResponse handleRequest(HttpRequest request) {
if (request.method() == HttpMethods.GET) {
Uri uri = request.getUri();
if (uri.path().equals("/hello")) {
String name = Util.getOrElse(uri.query().get("name"), "Mister X");

return
HttpResponse.create()
.withEntity("Hello " + name + "!");
} else {
return
HttpResponse.create()
.withStatus(404)
.withEntity("Not found");
}
} else {
return
HttpResponse.create()
.withStatus(StatusCodes.METHOD_NOT_ALLOWED)
.withEntity("Unsupported method");
}
}

/** Adds authentication to an existing request */
public static HttpRequest addAuthentication(HttpRequest request) {
// unused here but just to show the shortcut
request.addHeader(Authorization.basic("username", "password"));
/**
* Adds authentication to an existing request
*/
public static HttpRequest addAuthentication(HttpRequest request) {
// unused here but just to show the shortcut
request.addHeader(Authorization.basic("username", "password"));

return request
.addHeader(Authorization.create(HttpCredentials.createBasicHttpCredentials("username", "password")));
return request
.addHeader(Authorization.create(HttpCredentials.createBasicHttpCredentials("username", "password")));

}
}

/** Removes cookies from an existing request */
public static HttpRequest removeCookies(HttpRequest request) {
return request.removeHeader("Cookie");
}
/**
* Removes cookies from an existing request
*/
public static HttpRequest removeCookies(HttpRequest request) {
return request.removeHeader("Cookie");
}

/** Build a uri to send a form */
public static Uri createUriForOrder(String orderId, String price, String amount) {
return Uri.create("/order").query(
Query.create(
Pair.create("orderId", orderId),
Pair.create("price", price),
Pair.create("amount", amount)));
}
/**
* Build a uri to send a form
*/
public static Uri createUriForOrder(String orderId, String price, String amount) {
return Uri.create("/order").query(
Query.create(
Pair.create("orderId", orderId),
Pair.create("price", price),
Pair.create("amount", amount)));
}

public static Query addSessionId(Query query) {
return query.withParam("session", "abcdefghijkl");
}
public static Query addSessionId(Query query) {
return query.withParam("session", "abcdefghijkl");
}

public static Object accessScalaDefinedJavadslContentTypeAndMediaType(ContentType type) {
Object anything = null;

akka.http.javadsl.model.MediaType mediaType = type.mediaType();

// just for the sake of explicitly touching the interfaces
if (mediaType.binary()) anything = (akka.http.javadsl.model.MediaType.Binary) mediaType;
if (1 == 2) anything = (akka.http.javadsl.model.MediaType.Multipart) mediaType;
if (1 == 2) anything = (akka.http.javadsl.model.MediaType.WithOpenCharset) mediaType;
if (1 == 2) anything = (akka.http.javadsl.model.MediaType.WithFixedCharset) mediaType;

if (type.binary()) anything = (akka.http.javadsl.model.ContentType.Binary) type;
if (1 == 2) anything = (akka.http.javadsl.model.ContentType.NonBinary) type;
if (1 == 2) anything = (akka.http.javadsl.model.ContentType.WithCharset) type;
if (1 == 2) anything = (akka.http.javadsl.model.ContentType.WithFixedCharset) type;

return anything;
}
}

0 comments on commit 2925bea

Please sign in to comment.