// Path: common/src/main/java/io/github/cdelmas/spike/common/auth/AccessTokenVerificationCommandFactory.java
// public class AccessTokenVerificationCommandFactory {
// 
//     public AccessTokenVerificationCommand createVerificationCommand(String accessToken) {
//         return new FacebookAccessTokenVerificationCommand(accessToken);
//     }
// 
// }
// 
// Path: common/src/main/java/io/github/cdelmas/spike/common/domain/Car.java
// public class Car {
// 
//     private Integer id;
//     private String name;
// 
//     public Car(String name) {
//         this.name = name;
//     }
// 
//     Car() {
// 
//     }
// 
//     public void setId(Integer id) {
//         this.id = id;
//     }
// 
//     public void setName(String name) {
//         this.name = name;
//     }
// 
//     public Integer getId() {
//         return id;
//     }
// 
//     public String getName() {
//         return name;
//     }
// }
// 
// Path: common/src/main/java/io/github/cdelmas/spike/common/domain/CarRepository.java
// public interface CarRepository {
// 
//     Optional<Car> byId(int id);
// 
//     List<Car> all();
// 
//     void save(Car car);
// 
//     void update(Car car);
// 
//     void delete(Car car);
// }
// 
// Path: common/src/main/java/io/github/cdelmas/spike/common/persistence/InMemoryCarRepository.java
// @Singleton
// public class InMemoryCarRepository implements CarRepository {
// 
//     private final AtomicInteger idGenerator = new AtomicInteger(1);
//     private final Map<Integer, Car> repository = new ConcurrentHashMap<>();
// 
//     @Override
//     public Optional<Car> byId(int id) {
//         return Optional.ofNullable(repository.get(id));
//     }
// 
//     @Override
//     public List<Car> all() {
//         return new ArrayList<>(repository.values());
//     }
// 
//     @Override
//     public void save(Car car) {
//         if (car.getId() == null) {
//             car.setId(idGenerator.getAndIncrement());
//         }
//         repository.put(car.getId(), car);
//     }
// 
//     @Override
//     public void update(Car car) {
//         save(car);
//     }
// 
//     @Override
//     public void delete(Car car) {
//         repository.remove(car.getId());
//     }
// }
// 
// Path: common/src/main/java/io/github/cdelmas/spike/common/hateoas/Link.java
// public static Link self(String href) {
//     return new Link("self", href);
// }


import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import io.github.cdelmas.spike.common.auth.AccessTokenVerificationCommandFactory;
import io.github.cdelmas.spike.common.domain.Car;
import io.github.cdelmas.spike.common.domain.CarRepository;
import io.github.cdelmas.spike.common.persistence.InMemoryCarRepository;
import spark.Request;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Arrays;
import java.util.List;
import static io.github.cdelmas.spike.common.hateoas.Link.self;
import static java.util.stream.Collectors.toList;
import static spark.Spark.before;
import static spark.Spark.get;
import static spark.Spark.halt;
import static spark.Spark.post;
import static spark.SparkBase.port;
import static spark.SparkBase.secure;
/*
   Copyright 2015 Cyril Delmas

   Licensed 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 io.github.cdelmas.spike.sparkjava;




public class Main {
    public static void main(String[] args) {
        port(8099); // defaults on 4567
        ObjectMapper objectMapper = createObjectMapper();
        configureUnirest(objectMapper);
        secureServer();

        AccessTokenVerificationCommandFactory accessTokenVerificationCommandFactory = new AccessTokenVerificationCommandFactory();
        AuthenticationFilter authenticationFilter = new AuthenticationFilter(accessTokenVerificationCommandFactory);
        before(authenticationFilter::filter);

        HelloResource helloResource = new HelloResource();
        get("/hello", helloResource::hello);

        CarRepository carRepository = new InMemoryCarRepository();
        CarsResource carsResource = new CarsResource(carRepository, objectMapper);
        get("/cars", "application/json", carsResource::all, objectMapper::writeValueAsString);
        get("/cars/:id", "application/json", carsResource::byId, objectMapper::writeValueAsString);
        post("/cars", "application/json", carsResource::createCar);
    }



    private static void secureServer() {
        String keystoreFile = System.getProperty("javax.net.ssl.keyStorePath");
        String keystorePassword = System.getProperty("javax.net.ssl.keyStorePassword");
        String truststoreFile = System.getProperty("javax.net.ssl.trustStore");
        String truststorePassword = System.getProperty("javax.net.ssl.trustStorePassword");
// Path: common/src/main/java/io/github/cdelmas/spike/common/auth/AccessTokenVerificationCommandFactory.java
// public class AccessTokenVerificationCommandFactory {
// 
//     public AccessTokenVerificationCommand createVerificationCommand(String accessToken) {
//         return new FacebookAccessTokenVerificationCommand(accessToken);
//     }
// 
// }
// 
// Path: common/src/main/java/io/github/cdelmas/spike/common/domain/Car.java
// public class Car {
// 
//     private Integer id;
//     private String name;
// 
//     public Car(String name) {
//         this.name = name;
//     }
// 
//     Car() {
// 
//     }
// 
//     public void setId(Integer id) {
//         this.id = id;
//     }
// 
//     public void setName(String name) {
//         this.name = name;
//     }
// 
//     public Integer getId() {
//         return id;
//     }
// 
//     public String getName() {
//         return name;
//     }
// }
// 
// Path: common/src/main/java/io/github/cdelmas/spike/common/domain/CarRepository.java
// public interface CarRepository {
// 
//     Optional<Car> byId(int id);
// 
//     List<Car> all();
// 
//     void save(Car car);
// 
//     void update(Car car);
// 
//     void delete(Car car);
// }
// 
// Path: common/src/main/java/io/github/cdelmas/spike/common/persistence/InMemoryCarRepository.java
// @Singleton
// public class InMemoryCarRepository implements CarRepository {
// 
//     private final AtomicInteger idGenerator = new AtomicInteger(1);
//     private final Map<Integer, Car> repository = new ConcurrentHashMap<>();
// 
//     @Override
//     public Optional<Car> byId(int id) {
//         return Optional.ofNullable(repository.get(id));
//     }
// 
//     @Override
//     public List<Car> all() {
//         return new ArrayList<>(repository.values());
//     }
// 
//     @Override
//     public void save(Car car) {
//         if (car.getId() == null) {
//             car.setId(idGenerator.getAndIncrement());
//         }
//         repository.put(car.getId(), car);
//     }
// 
//     @Override
//     public void update(Car car) {
//         save(car);
//     }
// 
//     @Override
//     public void delete(Car car) {
//         repository.remove(car.getId());
//     }
// }
// 
// Path: common/src/main/java/io/github/cdelmas/spike/common/hateoas/Link.java
// public static Link self(String href) {
//     return new Link("self", href);
// }


// Path: sparkjava/src/main/java/io/github/cdelmas/spike/sparkjava/Main.java
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import io.github.cdelmas.spike.common.auth.AccessTokenVerificationCommandFactory;
import io.github.cdelmas.spike.common.domain.Car;
import io.github.cdelmas.spike.common.domain.CarRepository;
import io.github.cdelmas.spike.common.persistence.InMemoryCarRepository;
import spark.Request;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Arrays;
import java.util.List;
import static io.github.cdelmas.spike.common.hateoas.Link.self;
import static java.util.stream.Collectors.toList;
import static spark.Spark.before;
import static spark.Spark.get;
import static spark.Spark.halt;
import static spark.Spark.post;
import static spark.SparkBase.port;
import static spark.SparkBase.secure;

/*
   Copyright 2015 Cyril Delmas

   Licensed 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 io.github.cdelmas.spike.sparkjava;




public class Main {
    public static void main(String[] args) {
        port(8099); // defaults on 4567
        ObjectMapper objectMapper = createObjectMapper();
        configureUnirest(objectMapper);
        secureServer();

        AccessTokenVerificationCommandFactory accessTokenVerificationCommandFactory = new AccessTokenVerificationCommandFactory();
        AuthenticationFilter authenticationFilter = new AuthenticationFilter(accessTokenVerificationCommandFactory);
        before(authenticationFilter::filter);

        HelloResource helloResource = new HelloResource();
        get("/hello", helloResource::hello);

        CarRepository carRepository = new InMemoryCarRepository();
        CarsResource carsResource = new CarsResource(carRepository, objectMapper);
        get("/cars", "application/json", carsResource::all, objectMapper::writeValueAsString);
        get("/cars/:id", "application/json", carsResource::byId, objectMapper::writeValueAsString);
        post("/cars", "application/json", carsResource::createCar);
    }



    private static void secureServer() {
        String keystoreFile = System.getProperty("javax.net.ssl.keyStorePath");
        String keystorePassword = System.getProperty("javax.net.ssl.keyStorePassword");
        String truststoreFile = System.getProperty("javax.net.ssl.trustStore");
        String truststorePassword = System.getProperty("javax.net.ssl.trustStorePassword");
        secure(keystoreFile, keystorePassword, truststoreFile, truststorePassword);