配置信息初始化和文件环境的准备
public class RepositoryDataInitImpl implements RepositoryDataInit
{
public List initData(ServletContextEvent sce)
{
RepositoryData repositoryData=new RepositoryDataImpl();
List list=(List)repositoryData.getRepositoryData();
List repositoryList=new ArrayList();
for(int i=0;i<=list.size()-1;i++)
{
RepositoryModel repositoryModel=new RepositoryModel();
Object[] o =(Object[])list.get(i);
EBizOrg eBizOrg=(EBizOrg)o[0];
EBizOrgService eBizOrgService=(EBizOrgService)o[1];
repositoryModel.setOrgId(eBizOrg.getOrgId());
repositoryModel.setPassWord(eBizOrg.getUserPwd());
repositoryModel.setSecurityInfo(eBizOrg.getSecurityInfo());
repositoryModel.setUrl(eBizOrg.getServeAddress()+eBizOrgService.getServiceInterface());
repositoryModel.setUserName(eBizOrg.getAccessUser());
repositoryModel.setServiceName(eBizOrgService.getId().getServiceName());
repositoryModel.setServiceInterface(eBizOrgService.getServiceInterface());
repositoryModel.setServiceClass(eBizOrgService.getServiceClass());
repositoryModel.setKeyFile(eBizOrg.getKeyFile());
repositoryModel.setJksPwd(eBizOrg.getJksPwd());
repositoryList.add(repositoryModel);
createProperties(repositoryModel,sce);//.properties文件产生(转移,导入?)
createKeyFile(repositoryModel, sce);//.jks文件产生(转移,导入?)
}
return repositoryList;
}
public void createKeyFile(RepositoryModel repositoryModel,ServletContextEvent sce)
{
String path=sce.getServletContext().getRealPath("/")+"WEB-INF\\classes\\com\\key\\xfire\\";
OutputStream out = null;
InputStream fin=null;
try {
out = new FileOutputStream(path+repositoryModel.getOrgId().trim()+".jks");
fin = repositoryModel.getKeyFile().getBinaryStream();
System.out.println(repositoryModel.getOrgId().trim()+".jks" +":"+ fin.available());
int firstChar = fin.read();
int length = fin.available();
//将其转换成二进制数据
byte[] data = new byte[length+1];
data[0] = (byte)firstChar;
int len=0;
while (( len= fin.read(data,1,length+1)) != -1)
{
out.write(data, 0, len+1);
}
fin.close();
out.flush();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void createProperties(RepositoryModel repositoryModel,ServletContextEvent sce)
{
String path=sce.getServletContext().getRealPath("/")+"WEB-INF\\classes\\"+repositoryModel.getSecurityInfo();
path=path.replaceAll("/", "\\\\");
Properties prop=new Properties();
try{
File f=new File(path);
f.createNewFile();
FileInputStream in=new FileInputStream(path);
prop.load(in);
in.close();
prop.setProperty("org.apache.ws.security.crypto.provider", "org.apache.ws.security.components.crypto.Merlin");
prop.setProperty("org.apache.ws.security.crypto.merlin.keystore.type", "jks");
prop.setProperty("org.apache.ws.security.crypto.merlin.keystore.password", repositoryModel.getJksPwd().trim());
prop.setProperty("org.apache.ws.security.crypto.merlin.file", "com/key/xfire/"+repositoryModel.getOrgId().trim()+".jks");
try {
prop.store(new FileOutputStream(path), null);
} catch (IOException e) {}
}
catch(Exception e)
{
System.out.print(e.getMessage());
}
}
public Map initAccount(List list)
{
Map<String, String> passwords= new HashMap<String, String>();
for(int i=0;i<list.size();i++)
{
RepositoryModel repositoryModel =(RepositoryModel)list.get(i);
passwords.put(repositoryModel.getUserName().trim(), repositoryModel.getPassWord().trim());
}
return passwords;
}
public List setRepository(String key,ServletContextEvent sce)
{
String[] temp=key.split("-");
RepositoryData repositoryData=new RepositoryDataImpl();
List list=(List)repositoryData.getNewRepositoryData(temp[0],temp[1]);
List repositoryList=new ArrayList();
for(int i=0;i<=list.size()-1;i++)
{
RepositoryModel repositoryModel=new RepositoryModel();
Object[] o =(Object[])list.get(i);
EBizOrg eBizOrg=(EBizOrg)o[0];
EBizOrgService eBizOrgService=(EBizOrgService)o[1];
repositoryModel.setOrgId(eBizOrg.getOrgId());
repositoryModel.setPassWord(eBizOrg.getUserPwd());
repositoryModel.setSecurityInfo(eBizOrg.getSecurityInfo());
repositoryModel.setUrl(eBizOrg.getServeAddress()+eBizOrgService.getServiceInterface());
repositoryModel.setUserName(eBizOrg.getAccessUser());
repositoryModel.setServiceName(eBizOrgService.getId().getServiceName());
repositoryModel.setServiceInterface(eBizOrgService.getServiceInterface());
repositoryModel.setServiceClass(eBizOrgService.getServiceClass());
repositoryModel.setKeyFile(eBizOrg.getKeyFile());
repositoryModel.setJksPwd(eBizOrg.getJksPwd());
repositoryList.add(repositoryModel);
createProperties(repositoryModel,sce);
createKeyFile(repositoryModel, sce);
}
return repositoryList;
}
public Map setAccount(List list)
{
Map<String, String> passwords= new HashMap<String, String>();
for(int i=0;i<list.size();i++)
{
RepositoryModel repositoryModel =(RepositoryModel)list.get(i);
passwords.put(repositoryModel.getUserName().trim(), repositoryModel.getPassWord().trim());
}
return passwords;
}
}
web服务类的注册
public class ObjectRepositoryImpl implements ObjectRepository
{
private static Map<String, Object> serviceContainer = new HashMap<String, Object>();
private static Map<String, String> passwords=new HashMap<String, String>();
public List data;
private static ServletContextEvent sce;
public static Map<String, Object> getServiceContainer() {
return serviceContainer;
}
public static void setServiceContainer(Map<String, Object> serviceContainer) {
ObjectRepositoryImpl.serviceContainer = serviceContainer;
}
public List getData() {
return data;
}
public void setData(List data) {
this.data = data;
}
@SuppressWarnings("unchecked")
public void init(ServletContextEvent sce)
{
if (!ObjectRepositoryImpl.serviceContainer.isEmpty())
{
ObjectRepositoryImpl.serviceContainer.clear();
}
if (!ObjectRepositoryImpl.passwords.isEmpty())
{
ObjectRepositoryImpl.passwords.clear();
}
data=new ArrayList();
RepositoryDataInit repositoryDataInit=new RepositoryDataInitImpl();
data= repositoryDataInit.initData(sce);
this.passwords=repositoryDataInit.initAccount(data);//data包括了.jks里面的storepass
this.sce=sce;
}
public void repository()
{
for (int i=0;i<data.size();i++)
{
RepositoryModel repositoryModel=(RepositoryModel)data.get(i);
createRepository(repositoryModel);
}
}
public void createRepository(RepositoryModel repositoryModel)
{
try {
String url=repositoryModel.getUrl();
String userName=repositoryModel.getUserName();
String path=repositoryModel.getSecurityInfo();
String orgId=repositoryModel.getOrgId();
String serviceName=repositoryModel.getServiceName();
String serviceClass=repositoryModel.getServiceClass();
//String serviceName="com.saleTicket.service.SaleTicketService";
//产生对象引用
Service srvcModel=null;
XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
Class cls = null;
try {
cls = Class.forName(serviceClass);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
srvcModel = new ObjectServiceFactory().create(cls);
Object o=(Object)factory.create(srvcModel,url);
Client client =null;
client = ((XFireProxy)Proxy.getInvocationHandler(o)).getClient();
regSecurity(client,userName,path);
serviceContainer.put(orgId+"-"+serviceName,o);
}
catch (MalformedURLException e) {
e.printStackTrace();
}
}
public void regSecurity(Client client,String userName,String path)
{
client.addOutHandler(new DOMOutHandler());
Properties properties =new Properties();
properties.setProperty(WSHandlerConstants.ACTION,WSHandlerConstants.SIGNATURE);
properties.setProperty(WSHandlerConstants.USER, userName);
properties.setProperty(WSHandlerConstants.PW_CALLBACK_CLASS,PasswordHandler.class.getName());
properties.setProperty(WSHandlerConstants.SIG_PROP_FILE,path);
properties.setProperty(WSHandlerConstants.SIG_KEY_ID,"IssuerSerial");
client.addOutHandler(new WSS4JOutHandler(properties));
long timeout = 2000;
HttpClientParams params = new HttpClientParams();
// 避免'Expect: 100-continue' handshake
params.setParameter(HttpClientParams.USE_EXPECT_CONTINUE,Boolean.FALSE);
//设置ws连接超时时间
params.setParameter(HttpClientParams.CONNECTION_MANAGER_TIMEOUT, timeout);
//client.setTimeout(5000);
client.setProperty(CommonsHttpMessageSender.HTTP_CLIENT_PARAMS,params);
}
public Object getService(String serviceId,boolean flag)
{
String key = serviceId;
Object obj =serviceContainer.get(key);
if (obj==null)
{
RepositoryDataInit repositoryDataInit=new RepositoryDataInitImpl();
List list= repositoryDataInit.setRepository(key,sce);
for(int i=0 ;i<list.size();i++)
{
RepositoryModel repositoryModel=(RepositoryModel)list.get(i);
createRepository( repositoryModel);
}
passwords.putAll(repositoryDataInit.initAccount(list));
obj =serviceContainer.get(key);
}
return obj;
}
public static void setPasswords(Map<String, String> passwords)
{
ObjectRepositoryImpl.passwords = passwords;
}
public static Map<String, String> getPasswords() {
return passwords;
}
}
调用web服务
public SaleTicketService getServiceHandle(String ycNo)
{
ObjectRepositoryImpl objectRepositoryImpl=new ObjectRepositoryImpl();
Object o=objectRepositoryImpl.getService(ycNo,true);
SaleTicketService srvc=(SaleTicketService)o;
return srvc;
}
分享到:
相关推荐
Spring作为一个强大的框架,提供了全面的依赖注入和面向切面编程能力,而XFire是早先的Web服务实现,它允许开发者轻松地将Java接口转换为SOAP Web服务。在本教程中,我们将深入探讨如何使用Spring和XFire编写Web服务...
"xfire+spring+安全认证"的主题聚焦于如何利用XFire和Spring框架来实现安全的Web服务。XFire是一个用于创建、消费和部署SOAP Web服务的Java库,而Spring框架则是一个广泛使用的全功能应用开发框架,提供了包括安全性...
2. **XFire**:XFire是Apache CXF的前身,它是一个轻量级的Web服务框架,支持JAX-WS标准。XFire提供了一种简单的方式来创建和消费Web服务,包括动态代理、自动WSDL生成和XML绑定等功能。在这个场景中,XFire用于构建...
在本实例中,Spring作为核心容器管理其他组件,如Hibernate和Struts,同时可能通过Spring的Web服务模块(Spring-WS)或与其他Web服务技术(如XFire)集成,来提供Web服务的创建和消费。 2. **Hibernate ORM**:...
Spring 是一个全面的后端开发框架,提供了依赖注入、面向切面编程、事务管理等功能,而 XFire 是一个基于Java的Web服务实现库,它简化了创建和消费Web服务的过程。 在"spring3.1+xfire1.26 全注解"这个项目中,...
本项目结合了三个关键组件:Web服务(WebService)、Spring框架和XFire,以实现高效、灵活的服务创建和调用。 **Web服务(WebService)**: Web服务是一种基于互联网的应用程序接口,它使用XML(可扩展标记语言)...
XFire(现已被Apache CXF项目合并)是早期的Web服务实现,它支持SOAP、WS-*规范,并提供了易于使用的API来创建和消费Web服务。开发者可以使用XFire快速地将Java方法暴露为Web服务,或者调用远程的Web服务。XFire通过...
在压缩包文件"xfire+spring"中,可能包含了示例代码、配置文件和文档,用于演示如何在Spring环境中配置和使用XFire实现安全的Web服务交互。这些资源对于学习和理解XFire与Spring的集成以及Web服务安全认证非常有价值...
标题中的"axis-jax-ws-xfire.rar_jax"提到了三个关键术语:Axis、JAX-WS和XFire,这些都是与Java Web服务(Web Service)开发密切相关的技术。让我们详细了解一下这些概念及其在Java环境中的应用。 1. **Axis**: ...
1. 支持一系列Web服务新标准,如JSR181(Web服务注解)、WSDL2.0、JAXB2(Java对象到XML绑定)和WS-Security(Web服务安全)等。 2. 使用Stax解释XML,提高了性能。XFire采用了Woodstox作为Stax的实现,进一步优化了...
Spring通过其`Spring-WS`模块提供了对Web服务的支持,但若需要更全面的Web服务功能,如WSDL第一类公民支持、JAX-WS规范实现等,CXF是一个更好的选择。Spring可以通过配置文件或Java配置来整合CXF,实现Web服务...
通过这篇实战指南,我们可以学习到如何结合Mule ESB和XFire来构建一个高效的分布式系统,实现服务之间的松耦合通信。这不仅提高了系统的可维护性和可扩展性,还降低了不同系统间集成的复杂度。同时,对于开发者来说...
4. **实现服务接口**:为每个服务接口编写具体实现,这将是实际处理请求并返回响应的地方。 5. **配置XFire**:配置XFire以暴露你的服务,这可能涉及到创建XML配置文件或使用Java代码进行设置。 6. **部署服务**:...
XFire(后被Apache CXF合并)是一个用于创建和消费SOAP Web服务的库,它支持WS-I基本 profiles,提供丰富的数据绑定和协议支持。Spring则以其依赖注入和面向切面编程为核心,简化了Java应用程序的开发,包括Web服务...