HandleJSON.java

3 篇文章 0 订阅

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;

               }

               

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
2023-07-12 15:07:04.838 WARN 16200 --- [nio-9100-exec-9] .m.m.a.ExceptionHandlerExceptionResolver : Failure in @ExceptionHandler com.peanut.common.exception.RRExceptionHandler#handleException(Exception) org.apache.catalina.connector.ClientAbortException: java.io.IOException: 你的主机中的软件中止了一个已建立的连接。 at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:353) at org.apache.catalina.connector.OutputBuffer.flushByteBuffer(OutputBuffer.java:783) at org.apache.catalina.connector.OutputBuffer.append(OutputBuffer.java:688) at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:388) at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:366) at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:96) at org.springframework.util.StreamUtils$NonClosingOutputStream.write(StreamUtils.java:287) at com.fasterxml.jackson.core.json.UTF8JsonGenerator._flushBuffer(UTF8JsonGenerator.java:2171) at com.fasterxml.jackson.core.json.UTF8JsonGenerator.flush(UTF8JsonGenerator.java:1184) at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:1009) at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:456) at org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:104) at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:290) at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:183) at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:78) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:135) at org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver.doResolveHandlerMethodException(ExceptionHandlerExceptionResolver.java:428) at org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver.doResolveException(AbstractHandlerMethodExceptionResolver.java:75) at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:142) at org.springframework.web.servlet.handler.HandlerExceptionResolverComposite.resolveException(HandlerExceptionResolverComposite.java:80) at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1327) at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1138) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1084) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
07-13

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值