1. 程式人生 > >bos webservice開發流程

bos webservice開發流程

1.釋出WebService

2.選擇釋出的方法


3.把生成的程式碼複製到對應目錄


4.文字開啟生成的wsdd檔案,複製內容


5.貼上內容到檔案server-config.wsdd


6.重啟伺服器下載wsdl檔案


7.修改檔案字尾為.wsdl,複製到eclipse工種根目錄


8.生成客戶端程式碼


9.配置伺服器地址


10.程式碼呼叫介面方法

package com.hhxh.weixun.easservice;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.xml.rpc.ServiceException;

import localhost.ormrpc.services.EASLogin.EASLoginProxy;
import localhost.ormrpc.services.EASLogin.EASLoginProxyServiceLocator;
import localhost.ormrpc.services.WSWeiXunWebService.WSWeiXunWebServiceSrvProxy;
import localhost.ormrpc.services.WSWeiXunWebService.WSWeiXunWebServiceSrvProxyServiceLocator;

import org.json.JSONException;
import org.json.JSONObject;
import org.restlet.data.Form;
import org.restlet.ext.json.JsonRepresentation;

import com.hhxh.weixun.utils.PropertiesUtil;
import client.WSContext;

/**
 * Copyright (C), 2015-2025 Hhxh Tech. Co., Ltd
 * 
 * 動態模組介面實現
 * 
 * @author dhy
 * 
 * date: 2015-01-29
 */
public class WeiXunEasServiceHelper{
	
	protected final static DateFormat DF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	protected final static DateFormat DF1 = new SimpleDateFormat("yyyy-MM-dd");
	
	
	/**
	 *  登陸eas
	 * @param form
	 * @return
	 * @throws JSONException
	 * @throws SQLException 
	 */
	public static JsonRepresentation easLogin(Form form) throws JSONException, SQLException {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("sessionid", "");
		String userName = dealNull(form.getFirstValue("userName"));
		String password = dealNull(form.getFirstValue("password"));
		
		EASLoginProxyServiceLocator loginLocator = new EASLoginProxyServiceLocator();
		try {
			EASLoginProxy loginProxy = loginLocator.getEASLogin();
			System.out.println("------ 開始登入伺服器 .... ");
			WSContext context = loginProxy.login(userName, password, PropertiesUtil.slnName, PropertiesUtil.dcName, PropertiesUtil.language, PropertiesUtil.dbType);
			String sessionId = context.getSessionId();
			if(sessionId!=null)
			{
				System.out.println("------ 登陸成功,SessionID:" + sessionId);
				jsonObject.put("sessionid", sessionId);
			}
		} catch (ServiceException | RemoteException e) {
			e.printStackTrace();
		}
		return new JsonRepresentation(jsonObject);
	}
	
	/**
	 * 退出eas
	 * @param form
	 * @return
	 * @throws JSONException
	 * @throws SQLException 
	 */
	public static JsonRepresentation easLoginOut(Form form) throws JSONException, SQLException {
		JSONObject jsonObject = new JSONObject();
		String userName = dealNull(form.getFirstValue("userName"));
		EASLoginProxyServiceLocator loginSrv = new EASLoginProxyServiceLocator();
		boolean out = false;
		try {
			EASLoginProxy loginClient = loginSrv.getEASLogin();
			out = loginClient.logout(userName, PropertiesUtil.slnName,  PropertiesUtil.dcName, PropertiesUtil.language);
		} catch (RemoteException | ServiceException e) {
			e.printStackTrace();
		}
		jsonObject.put("status", out==true?"1":"0");
		jsonObject.put("message", out==true?"退出成功":"退出失敗");
		return new JsonRepresentation(jsonObject);
	}
	
	/**
	 * 呼叫eas webservice介面方法
	 * @param form
	 * @return
	 * @throws JSONException
	 * @throws SQLException 
	 */
	public static JsonRepresentation invoke(Form form) throws JSONException, SQLException {
		String operateType = form.getFirstValue("operateType");
		if(operateType==null || operateType.trim().length()==0)
		{
			return new JsonRepresentation(retMsg("0","引數operateType不能為空"));
		}
		String sessionId = form.getFirstValue("sessionId");
		if(sessionId==null || sessionId.trim().length()==0)
		{
			return new JsonRepresentation(retMsg("0","引數sessionId不能為空"));
		}
		String param = form.getFirstValue("param");
		if(param==null)
		{
			return new JsonRepresentation(retMsg("0","引數param不能為空"));
		}
		WSWeiXunWebServiceSrvProxyServiceLocator bsdLocator = new WSWeiXunWebServiceSrvProxyServiceLocator();
		String retValues = null;
		try {
			WSWeiXunWebServiceSrvProxy proxy = bsdLocator.getWSWeiXunWebService();
			System.out.println(sessionId);
			Class clazz = proxy.getClass();
			Method method = clazz.getDeclaredMethod(operateType, String.class, String.class);
			retValues = (String)method.invoke(proxy,sessionId, param); 
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
			return new JsonRepresentation(retMsg("0","介面呼叫失敗,引數operateType錯誤"));
		} catch (ServiceException e) {
			e.printStackTrace();
			return new JsonRepresentation(retMsg("0","介面呼叫失敗"));
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			return new JsonRepresentation(retMsg("0","介面呼叫失敗,請確認是否已登陸"));
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
			return new JsonRepresentation(retMsg("0","介面呼叫失敗,請確認是否已登陸"));
		} catch (InvocationTargetException e) {
			e.printStackTrace();
			return new JsonRepresentation(retMsg("0","介面呼叫失敗,請確認是否已登陸"));
		}
		return new JsonRepresentation(retValues);
	}
	
    private static JSONObject retMsg(String status,String message)
    {
    	JSONObject reJson = new JSONObject();
		try
		{
			reJson.put("status", status);
			reJson.put("message", message);
		}
		catch (JSONException e1)
		{
			e1.printStackTrace();
		}
		return reJson;
    }
    
    private static String dealNull(String arg)
    {
    	if(arg==null || arg.trim().length()==0 || "null".equalsIgnoreCase(arg))
    	{
    		return "";
    	}else
    	{
    		return arg.toString();
    	}
    }
}