java 實現WebService介面呼叫
阿新 • • 發佈:2018-12-30
/** * 使用者角色查詢對外介面 * @param accountid 當前使用者工號 * @param type 查詢型別 type=1 查詢使用者在CMS的角色 * @param branchName 分支名稱 type為 2 時 必填,其它非必填 * @return 返回加密結果xml報文 */ public String queryAccountPermission(String accountid, String type, String branchName) { log.info("接收引數 WebAccountQuery queryAccountPermissio"); log.info(" accountid="+accountid+" type="+type+" branchName="+branchName); log.info("end="); try { String result =""; String message = ""; //檢測必填項引數是否為空 if(accountid == null || "".equals(accountid) || type == null || "".equals(type)){ result= CMSConstant.ONE ; message=parameterError; return sendReturn(result,message,null,accountid); } if(!CMSConstant.ONE.equals(type) && (branchName == null || "".equals(branchName))){ result=CMSConstant.ONE; message=parameterError; return sendReturn(result,message,null,accountid); } //驗證使用者所傳工號是否有效 Account account = accountService.queryAccount(accountid); if(null == account ){ result=CMSConstant.ONE; message=noAccount; return sendReturn(result,message,null,accountid); } StringBuffer subject = new StringBuffer(); // type 等於 1 時 查詢該使用者 在CMS的角色 if(CMSConstant.ONE.equals(type)){ List<CMRole> roleList = roleService.queryAccountRoleList(accountid); if(roleList.size() > 0 ){ result = CMSConstant.ZERO; message = success; subject.append(accountStr+accountid+accountStrs); subject.append(permissionStr); for (CMRole cmRole : roleList) { subject.append("<cmsPermission>"+cmRole.getName()+"</cmsPermission>"); } subject.append(permissionStrs); }else { result=CMSConstant.ONE; message="該使用者在配置管理系統暫未分配角色"; } } <pre name="code" class="java"><span style="white-space:pre"> </span>/** * 封裝返回值 * @param accountid 使用者工號 * @param message 返回描述 * @param result 返回碼 * @param subject 返回報文 * @return 返回值 */ public String sendReturn(String result,String message,StringBuffer subject ,String accountid){ return CommonUtil.createXML( result, message, subject,accountid); }
// 判斷返回值是否為空if(null == subject || "".equals(result)){ result=CMSConstant.ONE; message="無返回, 請檢查您的引數";}//進行返回String data = CommonUtil.createXML( result, message, subject,accountid);return data;} catch (Exception e) {e.printStackTrace();log.error(errorStr+e);return null;}}
<pre name="code" class="java"><span style="white-space:pre"> </span>/**
* 對外介面報文封裝,加密
* @param result 返回狀態
* @param message 返回描述
* @param subject 返回內容報文
* @param accountid 工號
* @return 返回加密後報文
*/
public static String createXML(String result,String message,StringBuffer subject,String accountid){
StringBuffer sb = new StringBuffer();
sb.append("<xml version='1.0' encoding='UTF-8'>");
sb.append("<root>");
sb.append("<resultCode>");
sb.append(result);
sb.append("</resultCode>");
sb.append("<resultMessage>");
sb.append(message);
sb.append("</resultMessage>");
if(null != subject){
sb.append(subject.toString());
}
sb.append("</root>");
sb.append("</xml>");
if(null == accountid){
accountid = "";
}
log.info("CMS對外介面 返回報文="+sb.toString());
String data = DESEncryptionUtil.encrypt(sb.toString(),accountid, " [email protected]#$%");//對外介面加密祕鑰
return data;
}
介面呼叫方法/** *提供對外加密方法 * @param data 加密資料 * @param accountid 使用者工號 * @param key 祕鑰 * @return 字串 */ public static String encrypt(String data,String accountid,String key){ <span style="white-space:pre"> </span> byte[] enk = hex(accountid,key); <span style="white-space:pre"> </span> byte[] encoded = encryptMode(enk,data.getBytes()); <span style="white-space:pre"> </span>return Base64.encode(encoded); }
<pre name="code" class="java">private Map<String, String> map;
private static final String METHODNAME = "methodName";
private static final String URL = "url";
private static final String TYPE = "type";
/**
* 呼叫cms介面傳引數
* @return xml
*/
public String query(){
try {
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(map.get(URL));
QName qn = new QName(map.get(METHODNAME));//WSDL裡面描述的介面名稱
call.setOperationName(qn);
call.addParameter(ACCOUNTID, org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);//介面的引數
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//設定返回型別
String result = null;
if(map.get(TYPE) != null && !"".equals(map.get(TYPE))){
call.addParameter(TYPE, org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);//介面的引數
result = (String)call.invoke(new Object[]{map.get(ACCOUNTID),map.get(TYPE)});
}else{
result = (String)call.invoke(new Object[]{map.get(ACCOUNTID)});
}
String key = "[email protected]#$%";
result = DESEncryptionUtil.decrypt(result, map.get(ACCOUNTID), key);
return result;
} catch (Exception e) {
System.err.println(e.toString());
return null;
}
/**
* 提供對外解密方法
* @param data
* @param accountid 工號
* @param key 密匙
* @return String
* @throws UnsupportedEncodingException
*/
public static String decrypt(String data,String accountid,String key) throws UnsupportedEncodingException{
byte[] enk = hex(accountid,key);
byte[] dataByte = Base64.decode(data);
byte[] srcBytes = decryptMode(enk,dataByte);
return new String(srcBytes,"UTF-8");
}
/**
*
* @param username 使用者名稱
* @param key 密匙
* @return byte[]
*/
public static byte[] hex(String username,String key){
String f = DigestUtils.md5Hex(username+key);
byte[] bkeys = new String(f).getBytes();
byte[] enk = new byte[24];
for (int i=0;i<24;i++){
enk[i] = bkeys[i];
}
return enk;
}
/**
*
* @param keybyte 為加密金鑰,長度為24位元組
* @param src 為加密後的緩衝區
* @return byte[]
*/
public static byte[] decryptMode(byte[] keybyte,byte[] src){
try {
//生成金鑰
SecretKey deskey = new SecretKeySpec(keybyte, ALGORITHM);
//解密
Cipher c1 = Cipher.getInstance(ALGORITHM);
c1.init(Cipher.DECRYPT_MODE, deskey);
return c1.doFinal(src);
} catch (java.security.NoSuchAlgorithmException e1) {
// TODO: handle exception
e1.printStackTrace();
}catch(javax.crypto.NoSuchPaddingException e2){
e2.printStackTrace();
}catch(java.lang.Exception e3){
e3.printStackTrace();
}
return null;
}
<pre name="code" class="java"> /**
* 查詢使用者在CMS的角色
* @param accountId 域賬號
* @param endpoint 介面地址
* @return 介面返回值xml
*/
public String queryCMSRole(String accountId,String endpoint){
map = new HashMap<String, String>();
map.put(ACCOUNTID, accountId);
map.put(URL, endpoint);
map.put(METHODNAME, "queryAccountPermission");
map.put(TYPE, "1");
return query();
}
/**
* 解析xml 查詢使用者在cms角色
* @param xml
* @return Set<Account>
*/
public static List<String> getRoleByCMS(String xml){
try {
List<String> roleList = new ArrayList<String>();
Document doc = DocumentHelper.parseText(xml);
Element node = doc.getRootElement();
Element root = node.element(ROOT);
Element resultCode = root.element(CODE);
if(resultCode.getText().equals(ConstantUtil.ZERO)){//0:成功
Element permissionlist = root.element("permissionList");
List<Element> list= permissionlist.elements("cmsPermission");
for (Element e : list) {
String role = e.getText();
if(role != null && !"".equals(role)){
roleList.add(role);
}
}
}
return roleList;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
測試對介面呼叫
public static void main(String[] args) {
<span style="white-space:pre"> </span>CMSInterface cms = new CMSInterface();
<span style="white-space:pre"> </span>String url = "http://localhost:8080/cms/webservice/foreignService";
<span style="white-space:pre"> </span>String xml = cms.queryCMSRole("123456", url);
<span style="white-space:pre"> </span>List<String> roles = XMLUtil.getRoleByCMS(xml);
}