/**
 * Copyright 2016 IBM Corp.
 * <p/>
 * 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
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * 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.ibm.mfp.sample.smsOTP;

import com.ibm.mfp.adapter.api.ConfigurationAPI;
import com.ibm.mfp.adapter.api.OAuthSecurity;
import com.ibm.mfp.server.registration.external.model.ClientData;
import com.ibm.mfp.server.security.external.resource.AdapterSecurityContext;
import io.swagger.annotations.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@SwaggerDefinition(
        info = @Info(
                description = "An API that let register phone number, so it can received SMS as One-Time Password.",
                version = "V8.0.0beta",
                title = "Phone registration",
                termsOfService = "IBM Terms and Conditions apply",
                contact = @Contact(
                        name = "Ishai Borovoy"
                ),
                license = @License(
                        name = "IBM Samples License"
                )
        )
)
@Path("/phone")
@Api(value = "An API for the SMS One-Time Password security check",
        description = "This REST Adapter is used for register the user phone number in the registration service.")
public class SMSOTPResource {

    @Context
    private HttpServletRequest request;

    @Context
    private HttpServletResponse response;

    @Context
    private AdapterSecurityContext securityContext;

    @Context
    private ConfigurationAPI configAPI;

    @Path("/register/{phoneNumber}")
    @POST
    @Produces("application/json")
    @OAuthSecurity(enabled = true)

    @ApiOperation(value = "Register a phone number",
            notes = "Register a phone number in the registration service for sending the OTP SMS code",
            httpMethod = "POST",
            response = Void.class
    )
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "OK",
                    response = String.class),
            @ApiResponse(code = 400, message = "Invalid Phone Number",
                    response = String.class),
            @ApiResponse(code = 401, message = "Not Authorized",
                    response = String.class),
            @ApiResponse(code = 500, message = "Cannot register phone number",
                    response = String.class)
    })
    public String registerPhoneNumber(@PathParam("phoneNumber") String phoneNumber) {
        if (!validate(phoneNumber)) {
            throw new BadRequestException(String.format("Phone number [%s] is invalid", phoneNumber));
        }

        //Getting client data from the security context
        ClientData clientData = securityContext.getClientRegistrationData();
        if (clientData == null) {
            throw new InternalServerErrorException("Register phone number currently allowed only from a device.");
        }

        //Store the phone number in registration service
        clientData.getProtectedAttributes().put(SMSOTPSecurityCheck.PHONE_NUMBER, phoneNumber);
        securityContext.storeClientRegistrationData(clientData);

        return "OK";
    }

    /**
     * Validate phone number format
     * @param phoneNumber the phone number to validate
     * @return true if phone number is valid
     */
    private boolean validate(String phoneNumber) {
        String regex = "^\\+?[0-9. ()-]{10,25}$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(phoneNumber);

        return matcher.matches();
    }
}




@@ -1198,18 +1198,25 @@
 = %22A
-n API that
+ JAX-RS API which
  let
@@ -1217,32 +1217,34 @@
 ch let register 
+a 
 phone number, so
@@ -1243,54 +1243,8 @@
 mber
-, so it can received SMS as One-Time Password.
 %22,%0A 
@@ -1311,26 +1311,26 @@
  = %22
-Phone registration
+SMS OTP JAX-RS API
 %22,%0A 
@@ -1650,59 +1650,31 @@
  = %22
-An API for the SMS One-Time Password security check
+Register a phone number
 %22,%0A 
@@ -1704,12 +1704,14 @@
 his 
-REST
+JAX-RS
  Ada
@@ -1719,19 +1719,27 @@
 ter 
-is used for
+contains API to let
  reg
@@ -1752,12 +1752,14 @@
 the 
-user
+mobile
  pho
@@ -1771,37 +1771,8 @@
 mber
- in the registration service.
 %22)%0Ap
@@ -3322,32 +3322,34 @@
 ption(%22Register 
+a 
 phone number cur
@@ -3375,16 +3375,23 @@
  from a 
+mobile 
 device.%22

Fix Swagger doc