4booking中使用的是生成stub类来作为客户端调用的方式的
比如4booking中租借设备成功时并且选择了使用serlab时,会调用serlab的功能进行commission:
4booking:
ReservationManagerImpl:publishEvent
spring.publishEvent(new ReservationNotificationEvent(this, reservation, type));//使用spring框架来发布事件
ReservationNotificationEvent extends ApplicationEvent//spring提供的事件基类
TWAReservationManagerImpl implements ApplicationListener//该类为一个事件监听类,该接口需要实现onApplicationEvent
publicvoid onApplicationEvent(ApplicationEvent applicationEvent) {
//在方法内部进行instance类型判断来确定是否是感兴趣的事件
if(applicationEvent instanceof ReservationNotificationEvent){
//dosomething
}
}
//具体调用serlab的方法,通过生成stub对象,进行基本配置(服务url地址,用户名密码认证)
protected String notifyNewReservation(TWAInfo twa, ReservationInfo reservation) throws AxisFault, RemoteException {
TWAServiceStub stub = new TWAServiceStub(twa.getTwaServiceUrl());
Options opt = stub._getServiceClient().getOptions();
Authenticator authenticator = new Authenticator();
List<String> auth = new ArrayList<String>();
auth.add(Authenticator.BASIC);
authenticator.setAuthSchemes(auth);
authenticator.setUsername(twa.getUserName());
authenticator.setPassword(twa.getPassword());
authenticator.setPreemptiveAuthentication(true);
opt.setProperty(HTTPConstants.AUTHENTICATE, authenticator);//这里用户名认证需要serlab端另外配置
NotifyNewReservation r = new NotifyNewReservation();
r.setReservationInfo(reservation);
NotifyNewReservationResponse response = stub.notifyNewReservation(r);//调用serlab的具体方法
String value = response.get_return();
log.debug("New reservation response: " + value);
return value;
}
serlab:参考笔记->Axis2和已有web项目集成
补充下用户名认证:
1.web.xml里面编写filter:
<filter>
<filter-name>reservationServiceAuthFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>reservationServiceAuthFilter</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>reservationServiceAuthFilter</filter-name>
<url-pattern>/services/*</url-pattern>
</filter-mapping>
2.在spring中编写filter的bean:
<bean id="reservationServiceAuthFilter" class="com.nokia.lacama.serlab.filter.ReservationServiceAuthFilter"/>
3.在类中进行认证处理:
public class ReservationServiceAuthFilter extends OncePerRequestFilter implements InitializingBean {
//该方法是由OncePerRequestFilter提供的抽象方法,由OncePerRequestFilter内部的doFilter来调用
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
String header = request.getHeader("Authorization");//这里可以看到认证参数传递的是放在request头中的
if ((header != null) && header.startsWith("Basic ")) {
byte[] base64Token = header.substring(6).getBytes(CHARSET);
Base64 base64 = new Base64();
String token = new String(base64.decode(base64Token), CHARSET);
String username = "";
String password = "";
int delim = token.indexOf(":");
if (delim != -1) {
username = token.substring(0, delim).trim();
password = token.substring(delim + 1).trim();
}
log.debug("Basic Authentication Authorization header found for user '" + username + "'");
boolean passed = doAuth(username, password);
if(passed) {
filterChain.doFilter(request, response);
} else {
doAuthFail(response);
}
} else {
doAuthFail(response);
log.error("No Authorization header in request");
}
}
}