Skip to content

Commit

Permalink
finagle/finagle-example: Re-enable checkstyle and fix errors
Browse files Browse the repository at this point in the history
Problem / Solution

Un-suppress checkstyle suppression for
finagle/finagle-example/src/main/java/com/twitter/finagle/example/java/http/.*.java
and fix resulting errors.

RB_ID=877045
  • Loading branch information
jcrossley authored and jenkins committed Oct 3, 2016
1 parent 538eaf5 commit f1ad6f3
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 27 deletions.
@@ -1,9 +1,14 @@
package com.twitter.finagle.example.java.http;

public final class CatService {
private final static CatsDB db = new CatsDB();

private CatService() {

}

private static final CatsDB DB = new CatsDB();

public static Cat find(int id) {
return db.get(id);
return DB.get(id);
}
}
Expand Up @@ -3,19 +3,23 @@
import java.util.HashMap;

public class CatsDB {
private final static HashMap<Integer, Cat> db = new HashMap<>();
private static final HashMap<Integer, Cat> DB = new HashMap<>();

/**
* Get the Cat for a given id.
*/
public Cat get(Integer id) {
if (db.isEmpty())
if (DB.isEmpty()) {
addExampleCats();
}

return db.get(id);
return DB.get(id);
}

private void addExampleCats() {
db.put(0, new Cat("Doug"));
db.put(1, new Cat("Ozzy"));
db.put(2, new Cat("Logan"));
db.put(3, new Cat("Dylan"));
DB.put(0, new Cat("Doug"));
DB.put(1, new Cat("Ozzy"));
DB.put(2, new Cat("Logan"));
DB.put(3, new Cat("Dylan"));
}
}
@@ -1,14 +1,16 @@
package com.twitter.finagle.example.java.http;

import java.nio.charset.StandardCharsets;

import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;

import com.twitter.finagle.Service;
import com.twitter.finagle.SimpleFilter;
import com.twitter.finagle.http.Request;
import com.twitter.finagle.http.Response;
import com.twitter.util.ExceptionalFunction;
import com.twitter.util.Future;
import java.nio.charset.StandardCharsets;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;

/**
* A simple Finagle filter that intercepts Exceptions and converts them to a more comprehensible HTTP Response.
Expand All @@ -24,7 +26,8 @@ public Response applyE(Throwable in) {
if (in instanceof NumberFormatException) {
resp.setStatus(HttpResponseStatus.BAD_REQUEST);
resp.setContent(
ChannelBuffers.wrappedBuffer(in.getMessage().getBytes(StandardCharsets.UTF_8)));
ChannelBuffers.wrappedBuffer(
in.getMessage().getBytes(StandardCharsets.UTF_8)));

return resp;
}
Expand Down
@@ -1,16 +1,23 @@
package com.twitter.finagle.example.java.http;

import static java.lang.Integer.parseInt;

import org.jboss.netty.buffer.ChannelBuffers;

import com.twitter.finagle.Service;
import com.twitter.finagle.http.Request;
import com.twitter.finagle.http.Response;
import com.twitter.finagle.http.Status;
import com.twitter.util.Future;
import org.jboss.netty.buffer.ChannelBuffers;

import static com.twitter.finagle.example.java.http.JsonUtils.toBytes;
import static java.lang.Integer.parseInt;

public final class Handlers {

private Handlers() {

}

static Service<Request, Response> echoHandler() {
return new Service<Request, Response>() {
public Future<Response> apply(Request request) {
Expand Down
@@ -1,5 +1,8 @@
package com.twitter.finagle.example.java.http;

import java.net.InetAddress;
import java.net.InetSocketAddress;

import com.twitter.finagle.Http;
import com.twitter.finagle.ListeningServer;
import com.twitter.finagle.Service;
Expand All @@ -8,20 +11,28 @@
import com.twitter.finagle.http.Response;
import com.twitter.util.Await;

import java.net.InetAddress;
import java.net.InetSocketAddress;
public final class HttpServer {

private static final InetSocketAddress ADDR = new InetSocketAddress(
InetAddress.getLoopbackAddress(), 8888);

public class HttpServer {
private HttpServer() {

private final static InetSocketAddress addr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 8888);
}

/**
* Start the server.
*/
public static void main(String[] args) throws Exception {
LoggingFilter accessLog = new LoggingFilter();
NullToNotFound nullFilter = new NullToNotFound();
HandleErrors errorsFilter = new HandleErrors();
Service<Request, Response> service = accessLog.andThen(nullFilter).andThen(errorsFilter).andThen(router());
Service<Request, Response> service = accessLog
.andThen(nullFilter)
.andThen(errorsFilter)
.andThen(router());

ListeningServer server = Http.server().serve(addr, service);
ListeningServer server = Http.server().serve(ADDR, service);

Await.ready(server);
}
Expand Down
Expand Up @@ -3,14 +3,22 @@
import com.fasterxml.jackson.databind.ObjectMapper;

public final class JsonUtils {
static ObjectMapper mapper = new ObjectMapper();
private static ObjectMapper mapper = new ObjectMapper();

private JsonUtils() {

}

/**
* Convert an Object to an array of bytes.
*/
public static byte[] toBytes(Object value) {
try {
return mapper.writeValueAsBytes(value);
} catch (Exception e) {
System.out.println(e.getMessage());
throw new IllegalArgumentException(String.format("Could not transform to bytes: %s", e.getMessage()));
throw new IllegalArgumentException(
String.format("Could not transform to bytes: %s", e.getMessage()));
}
}
}
@@ -1,5 +1,7 @@
package com.twitter.finagle.example.java.http;

import scala.Function0;

import com.twitter.finagle.Service;
import com.twitter.finagle.SimpleFilter;
import com.twitter.finagle.http.Request;
Expand All @@ -8,7 +10,6 @@
import com.twitter.util.Function;
import com.twitter.util.Future;
import com.twitter.util.Stopwatch$;
import scala.Function0;

/**
* A simple Finagle filter that logs the Request total time in milliseconds.
Expand Down
@@ -1,5 +1,7 @@
package com.twitter.finagle.example.java.http;

import java.util.Objects;

import com.twitter.finagle.Service;
import com.twitter.finagle.SimpleFilter;
import com.twitter.finagle.http.Request;
Expand All @@ -8,8 +10,6 @@
import com.twitter.util.Function;
import com.twitter.util.Future;

import java.util.Objects;

/**
* A simple Finagle filter that handles Responses with null and convert them to NotFound Responses.
*/
Expand All @@ -20,8 +20,9 @@ public Future<Response> apply(Request req, Service<Request, Response> service) {
return service.apply(req).map(new Function<Response, Response>() {
@Override
public Response apply(Response resp) {
if (Objects.equals(resp.contentString(), "null"))
if (Objects.equals(resp.contentString(), "null")) {
return Response.apply(Status.NotFound());
}

return resp;
}
Expand Down

0 comments on commit f1ad6f3

Please sign in to comment.