Spring Web Services-এ (Spring-WS) ক্লায়েন্ট এবং সার্ভার সাইড ইন্টারসেপ্টর তৈরি করা হলো একটি গুরুত্বপূর্ণ ফিচার, যা ওয়েব সার্ভিসের অনুরোধ এবং প্রতিক্রিয়াগুলি প্রক্রিয়াজাত করতে ব্যবহার করা হয়। ইন্টারসেপ্টর মূলত মেসেজগুলি ক্যাপচার করে এবং প্রয়োজনীয় লজিক প্রয়োগ করতে দেয়, যেমন লগিং, অথেন্টিকেশন, অথোরাইজেশন, ডাটা ভ্যালিডেশন ইত্যাদি।
১. ক্লায়েন্ট সাইড ইন্টারসেপ্টর
Spring-WS-এ ক্লায়েন্ট সাইডে ClientInterceptor ইন্টারফেস ব্যবহার করে ইন্টারসেপ্টর তৈরি করা হয়।
উদাহরণ:
import org.springframework.ws.client.support.interceptor.ClientInterceptor;
import org.springframework.ws.context.MessageContext;
public class CustomClientInterceptor implements ClientInterceptor {
@Override
public boolean handleRequest(MessageContext messageContext) throws Exception {
System.out.println("Client Request Intercepted");
// Custom logic here
return true; // Continue the execution
}
@Override
public boolean handleResponse(MessageContext messageContext) throws Exception {
System.out.println("Client Response Intercepted");
// Custom logic here
return true; // Continue the execution
}
@Override
public boolean handleFault(MessageContext messageContext) throws Exception {
System.out.println("Client Fault Intercepted");
// Custom logic here
return true; // Continue the execution
}
}
ইন্টারসেপ্টর যোগ করা:
import org.springframework.ws.client.core.WebServiceTemplate;
@Configuration
public class WebServiceClientConfig {
@Bean
public WebServiceTemplate webServiceTemplate() {
WebServiceTemplate template = new WebServiceTemplate();
template.setInterceptors(new ClientInterceptor[]{new CustomClientInterceptor()});
return template;
}
}
২. সার্ভার সাইড ইন্টারসেপ্টর
Spring-WS-এ সার্ভার সাইডে EndpointInterceptor ইন্টারফেস ব্যবহার করা হয়।
উদাহরণ:
import org.springframework.ws.server.endpoint.interceptor.EndpointInterceptor;
import org.springframework.ws.context.MessageContext;
public class CustomServerInterceptor implements EndpointInterceptor {
@Override
public boolean handleRequest(MessageContext messageContext, Object endpoint) throws Exception {
System.out.println("Server Request Intercepted");
// Custom logic here
return true; // Continue the execution
}
@Override
public boolean handleResponse(MessageContext messageContext, Object endpoint) throws Exception {
System.out.println("Server Response Intercepted");
// Custom logic here
return true; // Continue the execution
}
@Override
public boolean handleFault(MessageContext messageContext, Object endpoint) throws Exception {
System.out.println("Server Fault Intercepted");
// Custom logic here
return true; // Continue the execution
}
@Override
public void afterCompletion(MessageContext messageContext, Object endpoint, Exception ex) throws Exception {
System.out.println("After Completion Logic");
// Custom logic here
}
}
ইন্টারসেপ্টর যোগ করা:
import org.springframework.ws.config.annotation.WsConfigurerAdapter;
import org.springframework.ws.server.EndpointInterceptor;
import org.springframework.context.annotation.Configuration;
@Configuration
public class WebServiceServerConfig extends WsConfigurerAdapter {
@Override
public void addInterceptors(List<EndpointInterceptor> interceptors) {
interceptors.add(new CustomServerInterceptor());
}
}
ইন্টারসেপ্টর ব্যবহারের সম্ভাব্য প্রয়োগ:
- অথেন্টিকেশন এবং অথোরাইজেশন: অনুরোধ অনুমোদন চেক করা।
- লগিং এবং মনিটরিং: মেসেজ লগ করা বা পর্যালোচনা করা।
- ডাটা ভ্যালিডেশন: অনুরোধ ডেটার মান যাচাই করা।
- এক্সেপশন হ্যান্ডলিং: ত্রুটি পরিচালনা করা।
Spring-WS ইন্টারসেপ্টর কাস্টমাইজেশন এবং ডাটা প্রসেসিং-এর জন্য শক্তিশালী টুল প্রদান করে।
Read more