ibm-mobilefirst - 无法在 MobileFirst V8.0 适配器中初始化 AdaptersAPI 对象,这会导致 NullPointerException

标签 ibm-mobilefirst mobilefirst-adapters

我正在 MFP V8 中开发适配器。下面是我验证用户名和密码的代码:

        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";
        }

    }

当我提交挑战答案时,我在以下行中收到 NullPointerException:

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

因为 adaptersAPI 为空。我是否需要进行任何额外的配置才能使其正常工作?如何初始化 AdaptersAPI 对象?

注意:登录方法和安全检查都在同一个适配器中。

更新

我花了更多的时间对此进行了研究,并将代码更新为上面给出的,并观察到以下内容:

1. 当提交质询响应后调用 validateCredentials() 时,我会在 AdapterAPI 对象中获取 null 值。

2. 当我使用 mobilefirst swagger 工具调用 getResourceData() 时,我将获取 AdapterAPI 的对象。

最佳答案

我们无法将适配器 API 注入(inject)安全检查对象(根据设计 - 不是错误)。我们唯一能做的就是将 Adapter 中的逻辑提取到 Java 代码中,而不使用适配器 API。

关于ibm-mobilefirst - 无法在 MobileFirst V8.0 适配器中初始化 AdaptersAPI 对象,这会导致 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38578154/

相关文章:

android - Worklight 项目在禁用 'provide library resources' 时搜索 main.js 和 typematic.js

java - cordova/Mobile First 应用程序中的“主要版本 51 比 5 更新”

android - 处理 Android 的 session 超时 IBM Mobile First v8.0.2017012919

ibm-mobilefirst - 未捕获的类型错误 : Cannot use 'in' operator to search for 'SUPPORT_COOKIES' in null

testing - 如何在应用程序外部测试 MobileFirst Adapter API

ibm-mobilefirst - IBM MobileFirst 中的 SQL 适配器调用失败

ios - 升级到 iOS 9 和 MobileFirst 7.1 后无法在设备或模拟器上登录应用程序

javascript - 从 IBM Mobilefirst http 适配器调用后端 POST Web 服务时出错

javascript - MobileFirst V8.0.0,cordova 客户端应用程序无法从 JavaScriptSOAP 获取响应(SOAP 集成)