2016-07-25 31 views
2

我正在MFP V8中开发适配器。下面是我的代码来验证用户名和密码:无法初始化导致NullPointerException的MobileFirst V8.0适配器中的AdaptersAPI对象

 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对象?

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

更新

我调查了更多的时间在它和代码更新到上面给出并观察以下几点:

validateCredentials()是越来越提交质询响应然后我叫后在AdapterAPI对象中获得null值。

2.因为,当我使用mobilefirst swagger工具调用getResourceData()时,我得到AdapterAPI的对象。

+0

我有确切的问题,有没有解决方案呢? –

+0

请参阅下面的答案。 –

回答

3

我们cannot inject the adapters API into a security check object(按设计 - 不是bug)。我们唯一的方法就是将适配器中的逻辑提取到Java代码中,而不使用适配器API。