// Path: reactive-lab-services/src/main/java/io/reactivex/lab/services/common/SimpleJson.java
// public class SimpleJson {
// 
//     private static final SimpleJson INSTANCE = new SimpleJson();
// 
//     private SimpleJson() {
// 
//     }
// 
//     public static Map<String, Object> jsonToMap(String jsonString) {
//         return INSTANCE._jsonToMap(jsonString);
//     }
// 
//     public static String mapToJson(Map<String, ?> map) {
//         return INSTANCE._mapToJson(map);
//     }
// 
//     private final ObjectMapper objectMapper = new ObjectMapper();
//     private final ObjectReader objectReader = objectMapper.reader(Map.class);
// 
//     private Map<String, Object> _jsonToMap(String jsonString) {
//         try {
//             return objectReader.readValue(jsonString);
//         } catch (IOException e) {
//             throw new RuntimeException("Unable to parse JSON", e);
//         }
//     }
// 
//     private final ObjectWriter objectWriter = objectMapper.writerWithType(Map.class);
// 
//     private String _mapToJson(Map<String, ?> map) {
//         try {
//             return objectWriter.writeValueAsString(map);
//         } catch (IOException e) {
//             throw new RuntimeException("Unable to write JSON", e);
//         }
//     }
// }


import com.netflix.eureka2.client.EurekaClient;
import io.reactivex.lab.services.common.SimpleJson;
import io.reactivex.netty.protocol.http.server.HttpServerRequest;
import io.reactivex.netty.protocol.http.server.HttpServerResponse;
import io.reactivex.netty.protocol.http.sse.ServerSentEvent;
import rx.Observable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
package io.reactivex.lab.services.impls;




public class SocialService extends AbstractMiddleTierService {

    public SocialService(EurekaClient client) {
        super("reactive-lab-social-service", client);
    }

    @Override
    protected Observable<Void> handleRequest(HttpServerRequest<?> request, HttpServerResponse<ServerSentEvent> response) {
        return Observable.from(request.getQueryParameters().get("userId")).map(userId -> {
            Map<String, Object> user = new HashMap<>();
            user.put("userId", userId);
            user.put("friends", Arrays.asList(randomUser(), randomUser(), randomUser(), randomUser()));
            return user;
// Path: reactive-lab-services/src/main/java/io/reactivex/lab/services/common/SimpleJson.java
// public class SimpleJson {
// 
//     private static final SimpleJson INSTANCE = new SimpleJson();
// 
//     private SimpleJson() {
// 
//     }
// 
//     public static Map<String, Object> jsonToMap(String jsonString) {
//         return INSTANCE._jsonToMap(jsonString);
//     }
// 
//     public static String mapToJson(Map<String, ?> map) {
//         return INSTANCE._mapToJson(map);
//     }
// 
//     private final ObjectMapper objectMapper = new ObjectMapper();
//     private final ObjectReader objectReader = objectMapper.reader(Map.class);
// 
//     private Map<String, Object> _jsonToMap(String jsonString) {
//         try {
//             return objectReader.readValue(jsonString);
//         } catch (IOException e) {
//             throw new RuntimeException("Unable to parse JSON", e);
//         }
//     }
// 
//     private final ObjectWriter objectWriter = objectMapper.writerWithType(Map.class);
// 
//     private String _mapToJson(Map<String, ?> map) {
//         try {
//             return objectWriter.writeValueAsString(map);
//         } catch (IOException e) {
//             throw new RuntimeException("Unable to write JSON", e);
//         }
//     }
// }


// Path: reactive-lab-services/src/main/java/io/reactivex/lab/services/impls/SocialService.java
import com.netflix.eureka2.client.EurekaClient;
import io.reactivex.lab.services.common.SimpleJson;
import io.reactivex.netty.protocol.http.server.HttpServerRequest;
import io.reactivex.netty.protocol.http.server.HttpServerResponse;
import io.reactivex.netty.protocol.http.sse.ServerSentEvent;
import rx.Observable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

package io.reactivex.lab.services.impls;




public class SocialService extends AbstractMiddleTierService {

    public SocialService(EurekaClient client) {
        super("reactive-lab-social-service", client);
    }

    @Override
    protected Observable<Void> handleRequest(HttpServerRequest<?> request, HttpServerResponse<ServerSentEvent> response) {
        return Observable.from(request.getQueryParameters().get("userId")).map(userId -> {
            Map<String, Object> user = new HashMap<>();
            user.put("userId", userId);
            user.put("friends", Arrays.asList(randomUser(), randomUser(), randomUser(), randomUser()));
            return user;
        }).flatMap(list -> response.writeStringAndFlush("data: " + SimpleJson.mapToJson(list) + "\n"))