From defceef8afef538555c13d33e344a89a828a3d97 Mon Sep 17 00:00:00 2001 From: Thomas Lenz Date: Fri, 20 Dec 2013 12:35:28 +0100 Subject: inital --- src/main/java/at/gv/util/client/ur/URClient.java | 307 +++++++++++++++++++++++ 1 file changed, 307 insertions(+) create mode 100644 src/main/java/at/gv/util/client/ur/URClient.java (limited to 'src/main/java/at/gv/util/client/ur/URClient.java') diff --git a/src/main/java/at/gv/util/client/ur/URClient.java b/src/main/java/at/gv/util/client/ur/URClient.java new file mode 100644 index 0000000..4988a0f --- /dev/null +++ b/src/main/java/at/gv/util/client/ur/URClient.java @@ -0,0 +1,307 @@ +package at.gv.util.client.ur; + +import java.math.BigInteger; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import javax.annotation.Resource; +import javax.net.ssl.SSLContext; +import javax.xml.bind.JAXBElement; +import javax.xml.namespace.QName; +import javax.xml.ws.BindingProvider; +import javax.xml.ws.WebServiceContext; +import javax.xml.ws.handler.Handler; + +import org.apache.commons.lang.RandomStringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +import at.gv.util.xsd.ur.xmlsw.ResultCriteriaType; +import at.gv.util.xsd.ur.xmlsw.ResultRecord; +import at.gv.util.xsd.ur.xmlsw.SearchByExampleType; +import at.gv.util.xsd.ur.xmlsw.SearchCriteriaType; +import at.gv.util.xsd.ur.xmlsw.SearchResponseType; +import at.gv.util.DOMUtils; +import at.gv.util.LaxHostNameVerifier; +import at.gv.util.LoggingHandler; +import at.gv.util.MiscUtil; +import at.gv.util.client.szr.SZRSOAPHandler; +import at.gv.util.config.EgovUtilConfiguration; +import at.gv.util.ex.EgovUtilException; +import at.gv.util.wsdl.ur.URSuche; +import at.gv.util.wsdl.ur.URSucheService; +import at.gv.util.xsd.szr.pvp.PvpTokenType; +import at.gv.util.xsd.ur.pd.IdentificationType; +import at.gv.util.xsd.ur.pd.NichtNatuerlichePersonTyp; +import at.gv.util.xsd.ur.pd.PersonenDatenTyp; +import at.gv.util.xsd.ur.search.Funktion; +import at.gv.util.xsd.ur.search.ObjectFactory; +import at.gv.util.xsd.ur.search.SucheUnternehmenNachBpkRequest; +import at.gv.util.xsd.ur.search.SucheUnternehmenNachIdsRequest; +import at.gv.util.xsd.ur.search.SucheUnternehmenRequest; +import at.gv.util.xsd.ur.search.SucheUnternehmensdaten; + +import com.sun.xml.ws.developer.JAXWSProperties; + +public class URClient { + + @Resource + WebServiceContext wsContext; + + private EgovUtilConfiguration config = null; + private Logger log = LoggerFactory.getLogger(URClient.class); + private boolean logEnabled = true; + private boolean evbCheck = true; + + private URSuche urSuche = null; + + private final static String version = "V2.0"; + private List allowedKeys = null; + + private final static String EVB = "einzelvertretungsbefugt"; + + public URClient(EgovUtilConfiguration config, List allowedKeys, boolean evbCheck, boolean logEnabled) throws EgovUtilException { + MiscUtil.assertNotNull(config, "config"); + this.config = config; + this.allowedKeys = allowedKeys; + this.logEnabled = logEnabled; + this.evbCheck = evbCheck; + initialize(); + } + + public List searchByBpk(String vzbpk) throws URClientException { + SucheUnternehmenNachBpkRequest request = new SucheUnternehmenNachBpkRequest(); + ObjectFactory of = new ObjectFactory(); + request.setVersion(version); + request.setBpkWTUREncoded(vzbpk); + List results = searchByExample(of.createSucheUnternehmenNachBpkRequest(request), -1); + return getNichtNatuerlichePersonen(results); + } + + public List searchByRegisterNumber(String number, String type, int maxResults) throws URClientException { + SucheUnternehmenRequest request = new SucheUnternehmenRequest(); + ObjectFactory of = new ObjectFactory(); + request.setVersion(version); + SucheUnternehmensdaten sud = new SucheUnternehmensdaten(); + sud.setId(number); + sud.setIdArt(type); + request.setUnternehmensdaten(sud); + List results = searchByExample(of.createSucheUnternehmenRequest(request), maxResults); + return getNichtNatuerlichePersonen(results); + } + + public List searchByName(String name, String plz, int maxResults) throws URClientException { + SucheUnternehmenRequest request = new SucheUnternehmenRequest(); + ObjectFactory of = new ObjectFactory(); + request.setVersion(version); + SucheUnternehmensdaten sud = new SucheUnternehmensdaten(); + sud.setUntName(name); + if (MiscUtil.isNotEmpty(plz)) { + sud.setPlz(plz); + } + request.setUnternehmensdaten(sud); + List results = searchByExample(of.createSucheUnternehmenRequest(request), maxResults); + return getNichtNatuerlichePersonen(results); + } + + @SuppressWarnings("unchecked") + private List getNichtNatuerlichePersonen(List records) throws URClientException { + + if (records == null || records.size() == 0) { + return new ArrayList(); + } + List kurs = new ArrayList(); + for (ResultRecord rr : records) { + PersonenDatenTyp pdt = ((JAXBElement) rr.getAny().get(0)).getValue(); + NichtNatuerlichePersonTyp nnpt = ((JAXBElement) pdt.getPerson()).getValue(); + for (IdentificationType idt : nnpt.getIdentification()) { + if ("KUR".equals(idt.getType())) { + + // check evb + if (this.evbCheck) { + if (isEVB(pdt)) { + kurs.add(idt.getValue().getValue()); + log.debug("EVB found for " + nnpt.getVollerName()); + } else { + log.debug("Not EVB for " + nnpt.getVollerName()); + } + } else { + kurs.add(idt.getValue().getValue()); + } + } + } + } + return getNichtNatuerlichePerson(kurs); + } + + // check if person is einzelvertretungsbefugt + public static boolean isEVB(PersonenDatenTyp pdt) { + boolean isEVB = false; + for (Object content : pdt.getZusatzdaten().getContent()) { + if (content instanceof JAXBElement) { + JAXBElement jxb = (JAXBElement) content; + if (jxb.getDeclaredType().equals(PersonenDatenTyp.class)) { + PersonenDatenTyp pd = (PersonenDatenTyp) jxb.getValue(); + if (pd.getZusatzdaten() != null) { + for (Object c2 : pd.getZusatzdaten().getContent()) { + + if (c2 instanceof Element) { + Element fktElement = (Element) c2; + if ("Funktion".equals(fktElement.getLocalName())) { + NodeList nl = fktElement.getElementsByTagNameNS("http://statistik.at/namespace/ur/stammdaten/1#", "Vertretungsbefugnis"); + if (nl.getLength() > 0) { + String vbt = DOMUtils.getText(nl.item(0)); + if (EVB.equals(vbt)) { + return true; + } + } + } + } else if (c2 instanceof JAXBElement) { + JAXBElement jxb2= (JAXBElement) c2; + if (jxb2.getDeclaredType().equals(Funktion.class)) { + Funktion fkt = (Funktion) jxb2.getValue(); + if (EVB.equals(fkt.getVertretungsbefugnis())) { + return true; + } + } + } + } + } + } + } + } + return false; + } + + // get list of legal persons + @SuppressWarnings("unchecked") + public List getNichtNatuerlichePerson(List ids) throws URClientException { + SucheUnternehmenNachIdsRequest request = new SucheUnternehmenNachIdsRequest(); + ObjectFactory of = new ObjectFactory(); + request.getKur().addAll(ids); + List results = searchByExample(of.createSucheUnternehmenNachIdsRequest(request), -1); + if (results.size() == 0) { + return new ArrayList(); + } + List result = new ArrayList(); + for (ResultRecord rr : results) { + PersonenDatenTyp pdt = ((JAXBElement)rr.getAny().get(0)).getValue(); + + NichtNatuerlichePersonTyp nnpt = ((JAXBElement) pdt.getPerson()).getValue(); + + // check if we have to filter out + if (this.allowedKeys == null || this.allowedKeys.size() == 0) { + result.add(nnpt); + } else { + boolean hasKey = false; + for (IdentificationType idt : nnpt.getIdentification()) { + if (this.allowedKeys.contains(idt.getType())) { + hasKey = true; + } + } + if (hasKey) { + result.add(nnpt); + } + } + + } + return result; + } + + // search by example + public List searchByExample(Object request, int maxResults) throws URClientException { + SearchByExampleType body = new SearchByExampleType(); + body.setSearchRequestId(createURSearchId()); + ResultCriteriaType rc = new ResultCriteriaType(); + // set only value of max results if > 0 + if (maxResults > 0) { + rc.setMaxRecords(BigInteger.valueOf(maxResults)); + } + body.setResultCriteria(rc); + SearchCriteriaType sct = new SearchCriteriaType(); + sct.getAny().add(request); + body.setSearchCriteria(sct); + SearchResponseType srt = this.urSuche.searchByExample(body); + + // check number of records found +// int numFound = 0; +// if (srt.getResultInfo() != null) { +// numFound = srt.getResultInfo().getReturnedRecords().intValue(); +// } + // check whether we have an error message + if (srt.getMessage() != null) { + if (srt.getMessage().getCode().intValue() != 2040) { // 2040 = no records found + throw new URClientException(srt.getMessage().getReason().get(0), srt.getMessage().getCode().intValue()); + } + } + if (srt.getResultRecords() == null || srt.getResultRecords().getResultRecord() == null) { + return new ArrayList(); + } + return srt.getResultRecords().getResultRecord(); + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + private void initialize() throws EgovUtilException { + URL url = URClient.class.getResource("/wsdl/ur/URSucheService.wsdl"); + URSucheService urService = null; + String urURL = null; + urService = new URSucheService(url, new QName("urn:at:statistik:udb:ws", "URSucheService")); + urSuche = urService.getURSucheService(); + if (config.isURTestEnvironment()) { + log.trace("Initializing UR test configuration."); + urURL = config.getURTestEnvironmentURL(); + } else { + log.trace("Initializing UR productive configuration."); + urURL = config.getURProductionEnvironmentURL(); + } + + log.trace("UR connection URL: " + urURL); + BindingProvider bindingProvider = (BindingProvider) urSuche; + Map requestContext = bindingProvider.getRequestContext(); + requestContext.put( + BindingProvider.ENDPOINT_ADDRESS_PROPERTY, urURL); + + log.trace("Adding JAX-WS request/response trace handler."); + List handlerList = bindingProvider.getBinding().getHandlerChain(); + if (handlerList == null) { + handlerList = new ArrayList(); + } + LoggingHandler loggingHandler = new LoggingHandler(); + if (this.logEnabled) { + handlerList.add(loggingHandler); + } + log.trace("Adding WS-Security Header handler."); + PvpTokenType pvpToken = config.getURPVPToken(); + SZRSOAPHandler szrSOAPHandler = new SZRSOAPHandler(); + szrSOAPHandler.configure(pvpToken); + handlerList.add(szrSOAPHandler); + bindingProvider.getBinding().setHandlerChain(handlerList); + + // check for ssl + if (urURL.toLowerCase().startsWith("https")) { + log.trace("Using ssl for SZR client request."); + SSLContext sslContext = this.config.getURsslConfiguration().getSSLContext(false); + if (sslContext == null) { + throw new EgovUtilException("SSL context from configuration is empty. Please configure an SSL context in the configuration first."); + } + requestContext.put(JAXWSProperties.SSL_SOCKET_FACTORY, sslContext.getSocketFactory()); + + // check for lax hostname + if (this.config.getURsslConfiguration().useLaxHostNameVerifier()) { + log.trace("LaxHostnameVerifier enabled. This setting is not recommended to use."); + requestContext.put(JAXWSProperties.HOSTNAME_VERIFIER, new LaxHostNameVerifier()); + } + } + } + + private String createURSearchId() { + return RandomStringUtils.randomAlphanumeric(8) + "-" + + RandomStringUtils.randomAlphanumeric(4) + "-" + RandomStringUtils.randomAlphanumeric(4) + "-" + + RandomStringUtils.randomAlphanumeric(12); + } + +} -- cgit v1.2.3