Unable to initialize AdaptersAPI Object in MobileFirst V8.0 adapter which is leading to NullPointerException

夙愿已清 提交于 2019-12-02 06:23:13

问题


I am developing the adapter in MFP V8. Below is my code to validate username and password:

        import java.util.HashMap;
        import java.util.Map; 
        import java.util.logging.Logger;

        import javax.ws.rs.GET;
        import javax.ws.rs.Path;
        import javax.ws.rs.Produces;
        import javax.ws.rs.core.Context;
        import javax.ws.rs.core.MediaType;

        import com.ibm.mfp.adapter.api.AdaptersAPI;
        import com.ibm.mfp.adapter.api.ConfigurationAPI;
        import com.ibm.mfp.security.checks.base.UserAuthenticationSecurityCheck;
        import com.ibm.mfp.server.registration.external.model.AuthenticatedUser;

        import io.swagger.annotations.Api;
        import io.swagger.annotations.ApiOperation;
        import io.swagger.annotations.ApiResponse;
        import io.swagger.annotations.ApiResponses;

        @Api(value = "Sample Adapter Resource")
        @Path("/resource")
        public class UserValidationSecurityCheck extends UserAuthenticationSecurityCheck{
            private String displayName;
            private String errorMsg;
            private HashMap<String,Object> adapterReponse = null; 
            @Context
            AdaptersAPI adaptersAPI;

            @Override
            protected AuthenticatedUser createUser() {
                return new AuthenticatedUser(displayName, displayName, this.getName(),adapterReponse);
            }

            @Override
            protected boolean validateCredentials(Map<String, Object> credentials) {
                if(credentials!=null && credentials.containsKey("username") && credentials.containsKey("password")){
                    if (credentials.get("username")!=null && credentials.get("password")!=null) {
                        String username = credentials.get("username").toString();
                        String password = credentials.get("password").toString();
                        if (username.equals(password)) {
                            JSONObject loginParams = new JSONObject();

                            loginParams.put("username", username);
                            loginParams.put("password", password);

                            HttpUriRequest httpUriRequest = adaptersAPI.createJavascriptAdapterRequest("LoginAndWeeklyCertAdapter1", "login", loginParams);
                            try {
                                HttpResponse httpResponse = adaptersAPI.executeAdapterRequest(httpUriRequest);
                                adapterReponse = adaptersAPI.getResponseAsJSON(httpResponse);
                                System.out.println(adapterReponse.toString());
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            return true;
                        } else {
                            errorMsg = "Wrong Credentials";
                        }
                    }
                }
                else{
                    errorMsg = "Credentials not set properly";
                }
                return false;
            }

            public boolean isLoggedIn(){
                return getState().equals(STATE_SUCCESS);
            }

            public AuthenticatedUser getRegisteredUser() {
                return registrationContext.getRegisteredUser();
            }

            @Override
            protected Map<String, Object> createChallenge() {
                Map<String, Object> challenge = new HashMap<String, Object>();
                challenge.put("errorMsg", errorMsg);
                challenge.put("remainingAttempts", getRemainingAttempts());
                return challenge;
            }

        @ApiOperation(value = "Returns 'Hello from resource'", notes = "A basic example of a resource returning a constant string.")
        @ApiResponses(value = { @ApiResponse(code = 200, message = "Hello message returned") })
        @GET
        @Produces(MediaType.TEXT_PLAIN)
        public String getResourceData() {
            // log message to server log
            logger.info("Logging info message...");

            return "Hello from resource";
        }

    }

When I am submitting the challenge answer I am getting NullPointerException in following line:

HttpUriRequest httpUriRequest = adaptersAPI.createJavascriptAdapterRequest("LoginAndWeeklyCertAdapter1", "login");

because adaptersAPI is null. Do I have to do any extra configuration in order to make that work? How can I initialize AdaptersAPI object?

Note: The login method and the security check both are in same adapter.

Update

I investigated more of time into it and updated the code to given above and observed the following:

1. When validateCredentials() is getting called after submitting the challenge response then I am getting null value in AdapterAPI object.

2. Where as, when I am calling the getResourceData() using the mobilefirst swagger tool then I am getting an object of AdapterAPI.


回答1:


We cannot inject the adapters API into a security check object(by design - not a bug). The only way we can go is to extract the logic from Adapter into a Java code, without using adapters API.



来源:https://stackoverflow.com/questions/38578154/unable-to-initialize-adaptersapi-object-in-mobilefirst-v8-0-adapter-which-is-lea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!