【问题标题】:BeanShell script error: bsh.ParseException: Parse error at line 126, column 23. Encountered: ,BeanShell 脚本错误:bsh.ParseException:第 126 行第 23 列的解析错误。遇到:,
【发布时间】:2021-06-20 20:23:41
【问题描述】:

我有一个在 Eclipse 上运行良好的代码,但是当我在 Sailpoint 工具(它是一个 beanshell 环境)上运行它时会出现以下错误:

Message key="sailpoint.tools.GeneralException: BeanShell script error: bsh.ParseException: Parse error at line 126, column 23.  Encountered: , BSF info: script at line: 0 column: columnNo" type="Error"/>

每次遇到Java Collection初始化时都会发生错误,例如:

  1. Map requestContext = wsdlProvider.getRequestContext();
  2. Map 字典 = (Map)javaScriptSerializer.readValue(input, new TypeReference>(){})

我需要在 Beanshell 中以其他语法声明变量吗?像List<String> roleNameValues = new ArrayList<String>(); 这样的简单初始化在 Beanshell 中运行良好。 请告知或建议如何在 Beanshell 中初始化 Map 之类的东西?

代码是:

public void configureServiceSession(BindingProvider wsdlProvider, String strServiceURL)
            throws MalformedURLException, IOException
    {
        // configure requestContext - WSDL URL and Session attribute
        Map<String, Object> requestContext = wsdlProvider.getRequestContext();

        requestContext.put( BindingProvider.ENDPOINT_ADDRESS_PROPERTY, 
                                            "http://" + server + ":" + port + strServiceURL );
        requestContext.put(BindingProvider.SESSION_MAINTAIN_PROPERTY, Boolean.TRUE);
        
        // Handle OAUTH2 Client
        TSSWSAPIHandler tssWSAPIHandler = new TSSWSAPIHandler();
        tssWSAPIHandler.setOAuthBearerToken(getOAuthToken("http://" + server + ":" + port + strSpotfireServerBase));
    
        List<Handler> handlerChain = new ArrayList<Handler>();
        handlerChain.add(tssWSAPIHandler);
        Binding bindObj = wsdlProvider.getBinding();
        bindObj.setHandlerChain(handlerChain);
    }

public String getOAuthToken(String strServerURL)
    {
        // config register-api-client --name APIImpersonateClient -Sapi.soap.impersonate
        // possible Scopes from com.spotfire.server.security.oauth.OAuthScopes class
        // "api.soap.library-service", "api.soap.update-analysis-service", "api.soap.information-model-service"
        // "api.soap.license-service", "api.soap.user-directory-service", "api.soap.impersonate";
        /*
        C:\tibco\tss\7.13.0\tomcat\bin>config register-api-client -n TestAPIClient -Sapi.soap.library-service -Sapi.soap.user-directory-service
        
        C:\tibco\tss\7.13.0\tomcat\bin>config register-api-client -n TestAPIClient -Sapi.soap.library-service -Sapi.soap.user-directory-service
        Tool password:
        Successfully registered a new API client with the display name 'TestAPIClient':
        <CLIENT ID and CLIENT SECRET OUTPUT>
        To view the full client configuration, please use the 'show-oauth2-client' command.
        */

        // New with api.soap.impersonate
        String oAuthClientID = clientID;
        String oAuthClientSecret = clientSecret;
        String accessToken = null;

        String urlOAuth = strServerURL + "/oauth2/token";

        try
        {
            System.out.println("Retrieving OAuth Token from: " + urlOAuth);

            URL wrOAuth = new URL(urlOAuth);
            HttpURLConnection wrInitial = (HttpURLConnection)wrOAuth.openConnection();

            String oAuthClientInfo = URLEncoder.encode(oAuthClientID, "UTF-8") + ":" + URLEncoder.encode(oAuthClientSecret, "UTF-8");
            String base64OAuth = Base64.getEncoder().encodeToString(oAuthClientInfo.getBytes("UTF-8"));

            wrInitial.setRequestProperty ("Authorization", "Basic " + base64OAuth);
            wrInitial.setRequestMethod("POST");
            wrInitial.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
            
            String strScopeInfo = "grant_type=client_credentials&scope=" + URLEncoder.encode("api.soap.library-service api.soap.user-directory-service", "UTF-8");
            byte[] postBytes = strScopeInfo.getBytes("UTF-8"); 

            wrInitial.setRequestProperty("Content-Length", "" + postBytes.length);
            wrInitial.setUseCaches(false);
            wrInitial.setDoInput(true);
            wrInitial.setDoOutput(true);   
            wrInitial.getOutputStream().write(postBytes);

            // read response information from request
            InputStreamReader responseStream = new InputStreamReader(wrInitial.getInputStream(), "UTF-8");

            Scanner sc = new Scanner(responseStream).useDelimiter("\\A");
            String input = sc.hasNext() ? sc.next() : "";
            // parse out JSON return data
            ObjectMapper javaScriptSerializer = new ObjectMapper();

            Map<String, Object> dictionary = (Map<String, Object>)javaScriptSerializer.readValue(input, new TypeReference<Map<String,Object>>(){});
            for (Map.Entry<String, Object> current : dictionary.entrySet())
            {
                String key;
                if ((key = current.getKey()) != null)
                {
                    if (key.equalsIgnoreCase("access_token"))
                    {
                        accessToken = (String)current.getValue();
                        continue;
                    }
                    if (key.equalsIgnoreCase("token_type"))
                    {
                        continue;
                    }
                    if (key.equalsIgnoreCase("expires_in"))
                    {
                        continue;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            System.err.println("Exception calling OAuth Token URL, " + urlOAuth + ": " + ex.getMessage());
        }
        return accessToken;
    }
    
 public List<String> getInitialCookies(String strServerURL) 
            throws IOException, MalformedURLException
    {
        List<String> listStrings = new ArrayList<String>();
        String strURLCalled = strServerURL + "/manifest";
        
        try 
        {
            URL urlCalled = new URL(strURLCalled);

            HttpURLConnection wrInitial = (HttpURLConnection) urlCalled.openConnection();
            wrInitial.setRequestMethod("GET");

            Map<String,List<String>> reqProps = wrInitial.getHeaderFields();

            listStrings = reqProps.get("Set-Cookie");
        }
        catch (Exception ex)
        {
            System.out.println("Exception calling initial URL, " + strURLCalled + ": " + ex.getMessage());
            throw ex;
        }

        return listStrings;
    }  

【问题讨论】:

    标签: java parsing collections beanshell parseexception


    【解决方案1】:

    删除 Beanshell 不支持的 Java 的 diamond operator &lt;&gt; 的使用

    例如创建没有泛型的地图:

     Map map = new HashMap();
    

    或者在你的情况下

    Map requestContext = wsdlProvider.getRequestContext();
    

    【讨论】:

      【解决方案2】:

      您可以不使用菱形运算符,也可以在 XML 文件的 SOURCE TAG 中使用 CDATA 并将其导入。这会将 java 支持的语法转换为 beanshell 的。

       <Source><![CDATA[
            //your code     
          List<String> roleNameValues = new ArrayList<String>(); 
        ]]></Source>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-09
        相关资源
        最近更新 更多