// Path: acmeair-customer-common/src/main/java/com/acmeair/entities/CustomerSession.java
// public interface CustomerSession {
// 
// 	String SESSIONID_COOKIE_NAME = "sessionid";
// 
// 	String getId();
// 
// 
// 	String getCustomerid();
// 
// 
// 	Date getLastAccessedTime();
// 	
// 
// 	Date getTimeoutTime();
// 
// 	
// }
// 
// Path: acmeair-customer-service/src/main/java/com/acmeair/service/CustomerService.java
// public interface CustomerService {
//     Customer createCustomer(String username, String password, Customer.MemberShipStatus status, int total_miles, int miles_ytd, String phoneNumber,
//                                    Customer.PhoneType phoneNumberType, CustomerAddressImpl address
//     );
//     
//     CustomerAddressImpl createAddress(String streetAddress1, String streetAddress2, String city, String stateProvince, String country, String postalCode);
//     
//     Customer updateCustomer(Customer customer);
//     
//     Customer getCustomerByUsername(String username);
//     
//     boolean validateCustomer(String username, String password);
//     
//     Customer getCustomerByUsernameAndPassword(String username, String password);
//     
//     CustomerSession validateSession(String sessionid);
//     
//     CustomerSession createSession(String customerId);
//     
//     void invalidateSession(String sessionid);
//     
//     Long count();
//     
//     Long countSessions();
// }


import com.acmeair.entities.CustomerSession;
import com.acmeair.entities.TokenInfo;
import com.acmeair.service.CustomerService;
import com.acmeair.web.dto.CustomerSessionInfo;
import io.servicecomb.provider.rest.common.RestSchema;
import io.servicecomb.swagger.invocation.exception.InvocationException;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import javax.ws.rs.Consumes;
import javax.ws.rs.CookieParam;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response.Status;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
/*******************************************************************************
* Copyright (c) 2013 IBM Corp.
*
* 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 com.acmeair.web;



@RestSchema(schemaId = "login")
@Path("/api/login")
public class LoginREST {
    public static final Logger logger = LoggerFactory.getLogger("LoginREST");

    @Autowired
    private CustomerService customerService;

    @POST
    @Consumes({MediaType.APPLICATION_FORM_URLENCODED})
    @Produces(MediaType.APPLICATION_JSON)
    @ApiResponses(value = { @ApiResponse(code = 403, message = "Invalid user information"),@ApiResponse(code = 500, message = "CustomerService Internal Server Error") })
    public TokenInfo login(@FormParam("login") String login, @FormParam("password") String password) {
        logger.info("Received login request of username [{}]", login);
        try {
            boolean validCustomer = customerService.validateCustomer(login, password);

            if (!validCustomer) {
                logger.info("No such user exists with username [{}]", login);
                throw new InvocationException(Status.FORBIDDEN, "username or password is wrong!");
            }

            logger.info("Validated user [{}] successfully", login);
// Path: acmeair-customer-common/src/main/java/com/acmeair/entities/CustomerSession.java
// public interface CustomerSession {
// 
// 	String SESSIONID_COOKIE_NAME = "sessionid";
// 
// 	String getId();
// 
// 
// 	String getCustomerid();
// 
// 
// 	Date getLastAccessedTime();
// 	
// 
// 	Date getTimeoutTime();
// 
// 	
// }
// 
// Path: acmeair-customer-service/src/main/java/com/acmeair/service/CustomerService.java
// public interface CustomerService {
//     Customer createCustomer(String username, String password, Customer.MemberShipStatus status, int total_miles, int miles_ytd, String phoneNumber,
//                                    Customer.PhoneType phoneNumberType, CustomerAddressImpl address
//     );
//     
//     CustomerAddressImpl createAddress(String streetAddress1, String streetAddress2, String city, String stateProvince, String country, String postalCode);
//     
//     Customer updateCustomer(Customer customer);
//     
//     Customer getCustomerByUsername(String username);
//     
//     boolean validateCustomer(String username, String password);
//     
//     Customer getCustomerByUsernameAndPassword(String username, String password);
//     
//     CustomerSession validateSession(String sessionid);
//     
//     CustomerSession createSession(String customerId);
//     
//     void invalidateSession(String sessionid);
//     
//     Long count();
//     
//     Long countSessions();
// }


// Path: acmeair-customer-service/src/main/java/com/acmeair/web/LoginREST.java
import com.acmeair.entities.CustomerSession;
import com.acmeair.entities.TokenInfo;
import com.acmeair.service.CustomerService;
import com.acmeair.web.dto.CustomerSessionInfo;
import io.servicecomb.provider.rest.common.RestSchema;
import io.servicecomb.swagger.invocation.exception.InvocationException;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import javax.ws.rs.Consumes;
import javax.ws.rs.CookieParam;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response.Status;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/*******************************************************************************
* Copyright (c) 2013 IBM Corp.
*
* 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 com.acmeair.web;



@RestSchema(schemaId = "login")
@Path("/api/login")
public class LoginREST {
    public static final Logger logger = LoggerFactory.getLogger("LoginREST");

    @Autowired
    private CustomerService customerService;

    @POST
    @Consumes({MediaType.APPLICATION_FORM_URLENCODED})
    @Produces(MediaType.APPLICATION_JSON)
    @ApiResponses(value = { @ApiResponse(code = 403, message = "Invalid user information"),@ApiResponse(code = 500, message = "CustomerService Internal Server Error") })
    public TokenInfo login(@FormParam("login") String login, @FormParam("password") String password) {
        logger.info("Received login request of username [{}]", login);
        try {
            boolean validCustomer = customerService.validateCustomer(login, password);

            if (!validCustomer) {
                logger.info("No such user exists with username [{}]", login);
                throw new InvocationException(Status.FORBIDDEN, "username or password is wrong!");
            }

            logger.info("Validated user [{}] successfully", login);
            CustomerSession session = customerService.createSession(login);