HandleJSON.java

package com.xxx.mobile.util;

 

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import java.util.Map;

 

import org.apache.commons.httpclient.util.URIUtil;

import org.openqa.selenium.remote.JsonToBeanConverter;

import org.openqa.selenium.remote.Response;

 

import com.xxx.mobile.util.sshserver.ClusterSsh;

import com.xxx.tahoe.testcase.Constant;

 

import jsystem.framework.report.Reporter;

import jsystem.framework.system.SystemObjectImpl; 


public class HandleJSON extends SystemObjectImpl{

               

               private String serverName="";

 

               private String sshKeyLoc="mailcontrol";

               private ClusterSsh cssh;

               private String accountNumber="86";

               private String policyName;

               private String policyDesc;

               private String profile;

               private String profileDesc;

               private String profilUrl="https://SERVERNAME:8085/os/v-1/account-ACCOUNTNUMBER/namespace-mobile/object-mobilepolicies/section-POLICYNAME/section-PROFILE/export";

               private String allPoliciesUrl="https://SERVERNAME:8085/os/v-1/account-ACCOUNTNUMBER/namespace-mobile/object-mobilepolicies/sections";

               private String commandHead="curl -H Accept:application/json --user'cn=mailcontrol (admin),account=1,dc=blackspider,dc=com:7Nz+BXcOXW3ZTXgZptPmRg'-k ";

               private String queryKeyAppendix=" |grep data | sed 's/\\\\n//g' |sed's/\"//g'|sed 's/,//g'|sed -n -r -e 's/^\\s+data\\s:\\s(.*)/\\1/p'|base64-d 2>/dev/null | sed 's/,/\\n/g'| sed 's/\"//g' |grep KEY | sed -n -r-e 's/.*KEY:(.*)/\\1/p'";

               private String queryIconAppendix=" |grep data | sed 's/\\\\n//g' |sed's/\"//g'|sed 's/,//g'|sed -n -r -e 's/^\\s+data\\s:\\s(.*)/\\1/p'|base64-d 2>/dev/null | sed 's/,/\\n/g'| sed 's/\"//g' |grep KEY | sed -n -r-e 's/.*KEY:(.{60}).*/\\1/p'";

               private String queryPoliciesAppendix=" 2>/dev/null | grep -e'\"KEY\"'|sed 's/\"//g'| sed -n -r -e's/^\\s+KEY\\s:\\s(.*)/\\1/p'|sed 's/,//g'|grep \"VALUE\"";

               

               public HandleJSON(ClusterSsh cssh,String serverName){

                               

                               this.cssh=cssh;

                               this.serverName=serverName;

                               

               }

               

               public HandleJSON(ClusterSsh cssh, String serverName, String accountID, StringpolicyName, String profile){

                               this.cssh=cssh;

                               this.serverName=serverName;

                               this.accountNumber=accountID;

//                           this.policyName=Constant.randomPolicyName;

//                           this.profile=Constant.randomProfileName;

               }

               

               

               public void connectServer()throws Exception{

                               

                               if (cssh.connectToHost(serverName)) {

                                               report.report("Connected to " + serverName);

                               } else {

                                               report.report("Failed to connect to " + serverName, Reporter.FAIL);

                               }

                               

                               cssh.becomeRoot();

               }

               

               public void connectServer(String serverName)throws Exception{

                               

                               if (cssh.connectToHost(serverName)) {

                                               report.report("Connected to " + serverName);

                               } else {

                                               report.report("Failed to connect to " + serverName, Reporter.FAIL);

                               }

                               

                               cssh.becomeRoot();

                               

               }

               

               

               public void disConnectServer()throws Exception{

                               

                               cssh.stopBeingRoot();

                               

                               cssh.disconnectFromHost();

                               

               }

               

               /**

               * Get payload item value according to key.

               * Not include profile name and description

               * @param key

               * @param type

               *                                          true - the key value is a boolean type

               *                                          false- the key value is not a boolean type

               * @return

               *                          if there is not value, return an empty string.

               * @throws Exception

               */

               public String queryValue(String key, boolean type) throws Exception{

                               

//                           String commandLine=this.composeQuery(key, type);

//                           report.report("Command line is: "+commandLine);

//                           String result=cssh.curl(commandLine);

//                           report.report("Result from JSON is: "+result);

                               String result=this.queryValue(key);

                               return result;

                               

               }

               

               /**

               * For All type. Get payload item value according to key.

               * @param key

               * @return

               * @throws Exception

               */

               public String queryValue(String key)throws Exception{

                               

                               String commandLine=this.composeQuery(key);

                               report.report("Command line is: "+commandLine);

                               String result=cssh.curl(commandLine);

                               result=this.manageString(result);

                               report.report("Result from JSON is: "+result);

                               return result;

               }

               

               public String queryIconValue(String key)throws Exception{

                               

                               String commandLine=this.composeIconQuery(key);

                               report.report("Command line is: "+commandLine);

                               String result=cssh.curl(commandLine);

                               result=this.manageString(result);

                               report.report("Result from JSON is: "+result);

                               return result;

               }

               

               /**

               * Check if the exact value exists in payload.

               * Good for check a key with several values.

                *            E.g. type

               *                          IosDeviceProfile

               *                           RestrictionPayload

               *                           WebClipPayload

                * @param key

               * @param value

               * @return

               *                          true - if it exists

               *                          false - if not exists

               * @throws Exception

               */

               public boolean isValueExist(String key, String value) throws Exception{

                               

                               String commandLine=this.composeQuery(key)+ " | grep \""+value+"\"";

                               report.report("Command line is: "+commandLine);

                               String result=cssh.curl(commandLine);

                               result=this.manageString(result);

                               report.report("Result from JSON is: "+result);

                               if(result.trim().equals(value)){

                                               return true;

                               }else{

                                               return false;

                               }

                               

               }

               

               /**

               * This is query all exists policy name from Object store.

               * Good for checking the default policy left after delete all policies.

               * @return

               * @throws Exception

               */

               public String queryAllPoliciesName() throws Exception{

                               

                               String commandLine=this.composeQueryAllPolicies();

                               report.report("Command line is: "+commandLine);

                               String result=cssh.curl(commandLine);

                               report.report("Result from JSON is: "+result);

                               return result;

               }

               

               /**

               * Check if policy name exist in Object Store

               * @return

               *                          Policy name string in server

               * @throws Exception

               */

               public boolean isPolicyNameExist() throws Exception{

                               

                               String commandLine=this.composeQueryPolicy("display_name",this.policyName);

                               report.report("Command line is: "+commandLine);

                               String result=cssh.curl(commandLine);

                               report.report("Result from JSON is: "+result);

                               if(result.trim().equals(this.policyName)){

                                               return true;

                               }else{

                                               return false;

                               }

                               

               }

               

               /**

               * Check if policy description exist in Object Store

               * @return

               *            Policydescription String in server

               * @throws Exception

               */

               public boolean isPolicyDescExist() throws Exception{

                               

                               String commandLine=this.composeQueryPolicy("description",this.policyDesc);

                               report.report("Command line is: "+commandLine);

                               String result=cssh.curl(commandLine);

                               report.report("Result from JSON is: "+result);

                               if(result.trim().equals(this.policyDesc)){

                                               return true;

                               }else{

                                               return false;

                               }

               }

               

               /**

               * Check profile name is correct in Object store.

               * @return

               *                          profile name string in JSON object

               * @throws Exception

               */

               public boolean isProfileNameCorrect() throws Exception{

                               

                               String commandLine=this.composeQueryProfile("display_name");

                               report.report("Command line is: "+commandLine);

                               String result=cssh.curl(commandLine);

                               report.report("Result from JSON is: "+result);

                               if(result.trim().equals(this.profile)){

                                               return true;

                               }else{

                                               return false;

                               }

               }

               

               /**

               * Get profile description from object store

               * @retrun

               *                          profile description string in JSON object

               * @throws Exception

               */

               public boolean isProfileDescCorrect() throws Exception{

                               

                               String commandLine=this.composeQueryProfile("description");

                               report.report("Command line is: "+commandLine);

                               String result=cssh.curl(commandLine);

                               report.report("Result from JSON is: "+result);

                               if(result.trim().equals(this.profileDesc)){

                                               return true;

                               }else{

                                               return false;

                               }

               }

               

               private String composeQueryProfile(String key) throws Exception{

                               

                              String queryProfile=this.composeCurl()+" | sed -n -r -e'/^\\s+\""+key+"\"/s/.*:\\s\"(.*)\"[,]{0,1}.*/\\1/p'";

                               return queryProfile;

               }

               

               private String composeQueryAllPolicies()throws Exception{

                               

                               String policy=this.allPoliciesUrl.replaceAll("SERVERNAME:8085",this.serverName+":8085");

                               policy=policy.replaceAll("ACCOUNTNUMBER", this.accountNumber);

                               String queryString=this.commandHead+policy+" 2>/dev/null | sed -n -r -e'/^\\s+\"name\"/s/.*:\\s\"(.*)\"[,]{0,1}.*/\\1/p'";

                               report.report(queryString);

                               return queryString;

               }

               

               private String composeQueryPolicy(String key,String value)throws Exception{

                               

                               String policy=this.allPoliciesUrl.replaceAll("SERVERNAME:8085",this.serverName+":8085");

                               policy=policy.replaceAll("ACCOUNTNUMBER", this.accountNumber);

                               String appendix=this.queryPoliciesAppendix.replaceAll("KEY",key);

                               appendix=appendix.replaceAll("VALUE", value);

                               String queryString=this.commandHead+policy+appendix;

                               return queryString;

               }

               

               /**

               * Compose query string and execute(Obsoleted)

               * @param key

               * @param type

               *                          - true  it's a boolean or number type key or null in Json

               *                          - false it's a string type key in Json

               * @return

               * @throws Exception

               */

               private String composeQuery(String key, boolean type)throws Exception{

                               

                               String temp="";

                               if (type){

                                               temp=this.composeCurl()+" | sed -n -r -e'/^\\s+\"data\"/s/.*"+key+"\\\\\":(true|false|null|[-]{0,1}[0-9]+)[,]{0,1}.*/\\1/p'";

                                               

                               }else{

                                               temp=this.composeCurl()+" | sed -n -r -e'/^\\s+\"data\"/s/.*"+key+"\\\\\": \\\\\"([0-9a-zA-Z\\.]+)\\\\\"[,]{0,1}.*/\\1/p'";

                               }

                               report.report(temp);

                               return temp;

               }

               

               /**

               * This is for query all type key-value in payload data

               * @param key

               * @return

               * @throws Exception

               */

               private String composeQuery(String key)throws Exception{

                               

                               String appendix=this.queryKeyAppendix.replaceAll("KEY", key);

                               String temp=this.composeCurl()+appendix;

                               return temp;

                               

               }

               

               private String composeIconQuery(String key) throws Exception{

                               

                               String appendix=this.queryIconAppendix.replaceAll("KEY", key);

                               String temp=this.composeCurl()+appendix;

                               return temp;

               }

               

               private String composeCurl()throws Exception{

                               

                               String tempURL=this.handleURL();

                               String composedCurl=this.commandHead+tempURL+" 2>/dev/null";

                               return composedCurl;

               }

               

               private String handleURL() throws Exception{

                               

                               String urlASCII=this.profilUrl.replaceAll("SERVERNAME:8085",this.serverName+":8085");

                               urlASCII=urlASCII.replaceAll("ACCOUNTNUMBER", this.accountNumber);

                               urlASCII=urlASCII.replaceAll("POLICYNAME", Constant.randomPolicyName);

                               urlASCII=urlASCII.replaceAll("PROFILE", Constant.randomProfileName);

                               String result=URIUtil.encodePath(urlASCII,"ISO-8859-1");

                               return result;

               }

               

               public String manageString(String a) {

                               

                               String temp=a;

                               int length=temp.length();

                               while(temp.endsWith("}") || temp.endsWith("]")){

                                               temp=temp.substring(0, length-1);

                                               length=temp.length();

                               }

                               return temp;

               }

 

               public String getServerName() {

                               return serverName;

               }

 

               public void setServerName(String serverName) {

                               this.serverName = serverName;

               }

 

               public String getSshKeyLoc() {

                               return sshKeyLoc;

               }

 

               public void setSshKeyLoc(String sshKeyLoc) {

                               this.sshKeyLoc = sshKeyLoc;

               }

 

               public String getPolicyName() {

                               return policyName;

               }

 

               public void setPolicyName(String policyName) {

                               this.policyName = policyName;

               }

 

               public String getProfile() {

                               return profile;

               }

 

               public void setProfile(String profile) {

                               this.profile = profile;

               }

 

               public String getPolicyDesc() {

                               return policyDesc;

               }

 

               public void setPolicyDesc(String policyDesc) {

                               this.policyDesc = policyDesc;

               }

 

               public String getProfileDesc() {

                               return profileDesc;

               }

 

               public void setProfileDesc(String profileDesc) {

                               this.profileDesc = profileDesc;

               }

 

               public String getAccountNumber() {

                               return accountNumber;

               }

 

               public void setAccountNumber(String accountNumber) {

                               this.accountNumber = accountNumber;

               }

               

}

{"code":"999999999","data":"com.alibaba.fastjson.JSONException: illegal input, offset 1, char [\n\tat com.alibaba.fastjson.JSON.parseObject(JSON.java:517)\n\tSuppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: \nError has been observed at the following site(s):\n\t|_ checkpoint ⇢ org.springframework.web.cors.reactive.CorsWebFilter [DefaultWebFilterChain]\n\t|_ checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]\n\t|_ checkpoint ⇢ org.springframework.boot.actuate.metrics.web.reactive.server.MetricsWebFilter [DefaultWebFilterChain]\n\t|_ checkpoint ⇢ HTTP GET \"/api/ecc/third/bank/list?coreCompanyCode=91320412MA20F9H3X3\" [ExceptionHandlingWebHandler]\nStack trace:\n\t\tat com.alibaba.fastjson.JSON.parseObject(JSON.java:517)\n\t\tat com.cdyfsz.base.svc.gateway.support.pre.response.ResponseProtocolAdaptionMachining.handle(ResponseProtocolAdaptionMachining.java:107)\n\t\tat com.cdyfsz.base.svc.gateway.support.pre.response.PreResponseHandler.preHandle(PreResponseHandler.java:52)\n\t\tat com.cdyfsz.base.svc.gateway.filters.pre.SurroundFilter$1.lambda$writeWith$1(SurroundFilter.java:228)\n\t\tat reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:100)\n\t\tat reactor.core.publisher.FluxBuffer$BufferExactSubscriber.onComplete(FluxBuffer.java:179)\n\t\tat reactor.core.publisher.FluxMap$MapSubscriber.onComplete(FluxMap.java:136)\n\t\tat reactor.core.publisher.FluxPeek$PeekSubscriber.onComplete(FluxPeek.java:252)\n\t\tat reactor.core.publisher.FluxMap$MapSubscriber.onComplete(FluxMap.java:136)\n\t\tat reactor.netty.channel.FluxReceive.terminateReceiver(FluxReceive.java:450)\n\t\tat reactor.netty.channel.FluxReceive.drainReceiver(FluxReceive.java:256)\n\t\tat reactor.netty.channel.FluxReceive.onInboundComplete(FluxReceive.java:387)\n\t\tat reactor.netty.channel.ChannelOperations.onInboundComplete(ChannelOperations.java:373)\n\t\tat reactor.netty.channel.ChannelOperations.terminate(ChannelOperati
最新发布
09-13
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值