package at.gv.util.client.mis.usp_v2; import java.net.URL; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import javax.annotation.Resource; import javax.net.ssl.SSLContext; import javax.xml.ws.BindingProvider; import javax.xml.ws.WebServiceContext; import javax.xml.ws.handler.Handler; import org.apache.commons.lang.RandomStringUtils; import org.apache.cxf.configuration.jsse.TLSClientParameters; import org.apache.cxf.endpoint.Client; import org.apache.cxf.frontend.ClientProxy; import org.apache.cxf.transport.http.HTTPConduit; import org.apache.cxf.transports.http.configuration.HTTPClientPolicy; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.gv.util.LaxHostNameVerifier; import at.gv.util.LoggingHandler; import at.gv.util.MiscUtil; import at.gv.util.client.szr.PVP2XHTTPHeaderHandler; import at.gv.util.config.EgovUtilConfiguration; import at.gv.util.ex.EgovUtilException; import at.gv.util.wsdl.mis.usp_v2.ISyncService; import at.gv.util.wsdl.mis.usp_v2.ListMandatesFault; import at.gv.util.wsdl.mis.usp_v2.SynchronisationServiceServiceagent; import at.gv.util.xsd.mis.usp_v2.eai.syncmsgs.ListMandatesRequest; import at.gv.util.xsd.mis.usp_v2.eai.syncmsgs.ListMandatesResponseType; import at.gv.util.xsd.mis.usp_v2.eai.syncmsgs.MandateAttributesSearchFilterType; import at.gv.util.xsd.mis.usp_v2.eai.syncmsgs.MandateList; import at.gv.util.xsd.mis.usp_v2.eai.syncmsgs.MandateListType; import at.gv.util.xsd.mis.usp_v2.eai.syncmsgs.ObjectFactory; import at.gv.util.xsd.mis.usp_v2.eai.syncmsgs.SearchCriteriaTestType; import at.gv.util.xsd.mis.usp_v2.eai.syncmsgs.SearchCriteriaType; import at.gv.util.xsd.mis.usp_v2.eai.syncmsgs.TimeRangeType; import at.gv.util.xsd.mis.usp_v2.mandates.Mandate; import at.gv.util.xsd.mis.usp_v2.persondata.IdentificationType; import at.gv.util.xsd.mis.usp_v2.persondata.IdentificationType.Value; import at.gv.util.xsd.szr.pvp.PvpTokenType; public class USPClient { public static final String USP_DATE_PATTERN = "yyyy-MM-dd"; @Resource WebServiceContext wsContext; private EgovUtilConfiguration config = null; private Logger log = LoggerFactory.getLogger(USPClient.class); private boolean logEnabled = true; private ISyncService uspSuche = null; private SimpleDateFormat formater; public USPClient(EgovUtilConfiguration config, boolean logEnabled) throws EgovUtilException { MiscUtil.assertNotNull(config, "config"); this.config = config; this.logEnabled = logEnabled; initialize(); } public List getMandates(String bpkType, String bpkValue, List mandateFilters) throws Exception { try { if (mandateFilters.size() > 1) log.debug("More than one mandate-profile required. Request each profile seperatly"); List results = new ArrayList(); for (String mandateFilter : mandateFilters) { log.trace("Building USP VDDS request ... "); ListMandatesRequest req = generateRequest(bpkType, bpkValue, mandateFilter); log.trace("Request pre-processing finished. Requesting USP ... "); ListMandatesResponseType resp = this.uspSuche.listMandates(req ); log.trace("Receive response from USP for filter:" + mandateFilter + " . Start post processing ... "); postProcessUSPMandateResult(resp, results); } return results; } catch (ListMandatesFault e) { log.warn("Receive an error from USP. Msg: " + e.getMessage(), e); throw e; } catch (Exception e) { log.error("Catch an generic error during USP communication.", e); throw e; } } private void postProcessUSPMandateResult(ListMandatesResponseType resp, List results) throws USPClientException { if (resp.getSearchResult() != null) { if (resp.getSearchResult().getDeclaredType().isAssignableFrom(MandateList.class)) { log.trace("Find 'MandateList' element in USP response"); MandateList mandateList = (MandateList) resp.getSearchResult().getValue(); if (mandateList.getMandates() != null && mandateList.getMandates().getMandate() != null) { log.debug("Find #" + mandateList.getMandates().getMandate().size() + " mandates in USP response"); results.addAll(mandateList.getMandates().getMandate()); } else log.info("Find 'null' element in USP response. Ignore full response"); } else log.warn("'SearchResult' element in USP response contains an unknown type: " + resp.getSearchResult().getName().toString()); } else { log.debug("'SearchResult' element in USP response is 'null' Inspecting response on errors ..."); if (resp.getResult() != null) { String errorCode = resp.getResult().getErrorCode(); String errorMsg = resp.getResult().getErrorMessage(); if (MiscUtil.isNotEmpty(errorCode) || MiscUtil.isNotEmpty(errorMsg)) { log.info("Receice an error from USP! Code: " + errorCode + " Msg: " + errorMsg); throw new USPClientException(errorMsg, errorCode); } } } } private ListMandatesRequest generateRequest(String bpkType, String bpkValue, String mandateFilter) { ObjectFactory objFactory = new ObjectFactory(); ListMandatesRequest parameters = new ListMandatesRequest(); MandateAttributesSearchFilterType searchFilterType = new MandateAttributesSearchFilterType(); SearchCriteriaTestType searchCriteriaTest = new SearchCriteriaTestType(); SearchCriteriaType searchCriteria = new SearchCriteriaType(); IdentificationType representative = new IdentificationType(); Value IdValue = new Value(); //set bPK IdValue.setValue(bpkValue); representative.setValue(IdValue ); representative.setType(bpkType); searchCriteria.getActivationRangeOrMandateTypeOrMandatorID().add(objFactory.createSearchCriteriaTypeRepresentativeID(representative)); //set allowed time range TimeRangeType fromTo = new TimeRangeType(); fromTo.setFrom(formater.format(new Date())); fromTo.setTo("9999-12-31"); searchCriteria.getActivationRangeOrMandateTypeOrMandatorID().add(objFactory.createSearchCriteriaTypeValidFromTo(fromTo)); //set mandate filters searchCriteria.getActivationRangeOrMandateTypeOrMandatorID().add(objFactory.createSearchCriteriaTypeMandateType(mandateFilter)); searchCriteriaTest.setSearchCriteria(searchCriteria); searchFilterType.setFilter(searchCriteriaTest); parameters.getSearchFilter().add(objFactory.createMandateAttributesSearchFilter(searchFilterType)); return parameters; } @SuppressWarnings({ "rawtypes", "unchecked" }) private void initialize() throws EgovUtilException { URL url = USPClient.class.getResource("/wsdl/mis/usp_v2/Synchronisation-1.wsdl"); SynchronisationServiceServiceagent uspService = null; String uspURL = null; uspService = new SynchronisationServiceServiceagent(url); uspSuche = uspService.getISyncServiceEndpoint(); if (config.isUSPTestEnvironment()) { log.trace("Initializing USP test configuration."); uspURL = config.getUSPTestEnvironmentURL(); } else { log.trace("Initializing USP productive configuration."); uspURL = config.getUSPProductionEnvironmentURL(); } log.trace("USP connection URL: " + uspURL); BindingProvider bindingProvider = (BindingProvider) uspSuche; Map requestContext = bindingProvider.getRequestContext(); requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, uspURL); log.trace("Adding JAX-WS request/response trace handler."); List handlerList = bindingProvider.getBinding().getHandlerChain(); if (handlerList == null) { handlerList = new ArrayList(); } // PV authentication log.trace("Adding WS-Security Header handler."); PvpTokenType pvpToken = config.getUSPPVPToken(); VDDSRoleGenerationStrategy roleStrategy = new VDDSRoleGenerationStrategy( config.getConfigurationValue("egovutil.usp.role.dynamic.participant"), config.getConfigurationValue("egovutil.usp.role.dynamic.secret")); PVP2XHTTPHeaderHandler szrSOAPHandler = new PVP2XHTTPHeaderHandler(roleStrategy); szrSOAPHandler.configure(pvpToken); handlerList.add(szrSOAPHandler); LoggingHandler loggingHandler = new LoggingHandler(); if (this.logEnabled) { handlerList.add(loggingHandler); } // set handler bindingProvider.getBinding().setHandlerChain(handlerList); Client client = ClientProxy.getClient(uspSuche); HTTPConduit http = (HTTPConduit) client.getConduit(); HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy(); //set HTTP Client Timeout (Default Timeout 60sec) httpClientPolicy.setConnectionTimeout(this.config.getHTTPRequestTimeout()); httpClientPolicy.setAllowChunking(false); httpClientPolicy.setReceiveTimeout(32000); http.setClient(httpClientPolicy); // check for ssl if (uspURL.toLowerCase().startsWith("https")) { log.trace("Using ssl for SZR client request."); SSLContext sslContext = this.config.getUSPsslConfiguration().getSSLContext(false); if (sslContext == null) { throw new EgovUtilException("SSL context from configuration is empty. Please configure an SSL context in the configuration first."); } TLSClientParameters tlsParams = new TLSClientParameters(); tlsParams.setSSLSocketFactory(sslContext.getSocketFactory()); // check for lax hostname if (this.config.getUSPsslConfiguration().useLaxHostNameVerifier()) { log.trace("LaxHostnameVerifier enabled. This setting is not recommended to use."); tlsParams.setHostnameVerifier(new LaxHostNameVerifier()); } http.setTlsClientParameters(tlsParams ); } formater = new SimpleDateFormat(USP_DATE_PATTERN); } private String createUSPRequestId() { return RandomStringUtils.randomAlphanumeric(12); } }