diff options
Diffstat (limited to 'src/main/java/at')
-rw-r--r-- | src/main/java/at/gv/util/ValidationUtils.java | 19 | ||||
-rw-r--r-- | src/main/java/at/gv/util/client/ur_V7/URClient.java | 590 | ||||
-rw-r--r-- | src/main/java/at/gv/util/client/ur_V7/URClientException.java | 33 |
3 files changed, 642 insertions, 0 deletions
diff --git a/src/main/java/at/gv/util/ValidationUtils.java b/src/main/java/at/gv/util/ValidationUtils.java index cbc7207..108fe27 100644 --- a/src/main/java/at/gv/util/ValidationUtils.java +++ b/src/main/java/at/gv/util/ValidationUtils.java @@ -2,11 +2,16 @@ package at.gv.util; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.Period; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.util.regex.Pattern; public class ValidationUtils { private static final String DATEFORMAT = "dd.MM.yyyy"; + private static final int REPRESENTATION_MIN_AGE = 14; public static boolean isDateFormat(String dateString) { @@ -57,5 +62,19 @@ public class ValidationUtils { return Pattern.compile("[0-9]{4}").matcher(postcode).matches(); } + + public static boolean isOverThirteen(String representativeDateOfBirth) { + try { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATEFORMAT); + LocalDate birthdate= LocalDate.parse(representativeDateOfBirth, formatter); + Period p = Period.between(birthdate, LocalDate.now()); + return p.getYears() >= REPRESENTATION_MIN_AGE; + + } catch (DateTimeParseException e) { + return false; + + } + + } } diff --git a/src/main/java/at/gv/util/client/ur_V7/URClient.java b/src/main/java/at/gv/util/client/ur_V7/URClient.java new file mode 100644 index 0000000..530951e --- /dev/null +++ b/src/main/java/at/gv/util/client/ur_V7/URClient.java @@ -0,0 +1,590 @@ +package at.gv.util.client.ur_V7; + +import java.math.BigInteger; +import java.net.URL; +import java.util.ArrayList; +import java.util.GregorianCalendar; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.annotation.Resource; +import javax.net.ssl.SSLContext; +import javax.xml.datatype.DatatypeConfigurationException; +import javax.xml.datatype.DatatypeConstants; +import javax.xml.datatype.DatatypeFactory; +import javax.xml.datatype.XMLGregorianCalendar; +import javax.xml.namespace.QName; +import javax.xml.ws.BindingProvider; +import javax.xml.ws.WebServiceContext; +import javax.xml.ws.WebServiceException; +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.pvp.rprofile.Pvp18pSoapHandler; +import at.gv.util.config.EgovUtilConfiguration; +import at.gv.util.ex.EgovUtilException; +import at.gv.util.wsdl.ur_V7.URSuche; +import at.gv.util.wsdl.ur_V7.URSucheService; +import at.gv.util.wsdl.ur_V7.XmlSwFault; +import at.gv.util.xsd.szr.pvp.PvpTokenType; +import at.gv.util.xsd.ur_V7.pd.IdentificationType; +import at.gv.util.xsd.ur_V7.pd.NichtNatuerlichePersonTyp; +import at.gv.util.xsd.ur_V7.pd.PersonenDatenTyp; +import at.gv.util.xsd.ur_V7.pd.PersonenDatenTyp.Zusatzdaten; +import at.gv.util.xsd.ur_V7.pd.PersonenDatenZusatzdatenTyp; +import at.gv.util.xsd.ur_V7.pd.ZusatzdatenPersonenTyp; +import at.gv.util.xsd.ur_V7.search.BestandszeitraumVollzug; +import at.gv.util.xsd.ur_V7.search.FktRegelTypVollzug; +import at.gv.util.xsd.ur_V7.search.FunktionVollzug; +import at.gv.util.xsd.ur_V7.search.SucheUnternehmenNachBpkRequest; +import at.gv.util.xsd.ur_V7.search.SucheUnternehmenNachIdRequest; +import at.gv.util.xsd.ur_V7.search.SucheUnternehmenRequest; +import at.gv.util.xsd.ur_V7.search.SucheUnternehmensdaten; +import at.gv.util.xsd.ur_V7.simpletypes.EvbStatusTyp; +import at.gv.util.xsd.ur_V7.xmlsw.ExtraResultCriteria; +import at.gv.util.xsd.ur_V7.xmlsw.ResultCriteriaType; +import at.gv.util.xsd.ur_V7.xmlsw.ResultRecord; +import at.gv.util.xsd.ur_V7.xmlsw.SearchByExampleType; +import at.gv.util.xsd.ur_V7.xmlsw.SearchCriteriaType; +import at.gv.util.xsd.ur_V7.xmlsw.SearchResponseType; + +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 = "V5.0"; + private List<String> allowedKeys = null; + private HashSet<String> allowedERsBKeys = null; + + public URClient(EgovUtilConfiguration config, List<String> allowedKeys, HashSet<String> ersbkeys, boolean evbCheck, boolean logEnabled) throws EgovUtilException { + MiscUtil.assertNotNull(config, "config"); + this.config = config; + this.allowedKeys = allowedKeys; + this.logEnabled = logEnabled; + this.evbCheck = evbCheck; + this.allowedERsBKeys = ersbkeys; + + initialize(); + } + + public List<URSearchResult> searchByRegisterNumberFullInfo(String number, String type, int maxResults) throws Exception { + + SucheUnternehmenNachIdRequest request = new SucheUnternehmenNachIdRequest(); + + if (type.equals("ERJ")) + request.setErj(number); + else if (type.equals("FBN")) + request.setFbn(number); + else if (type.equals("KUR")) + request.setKur(number); + else if (type.equals("SEK")) + request.setSek(number); + else if (type.equals("ZVR")) + request.setZvr(number); + else + throw new Exception("Wrong RegisterNumber Type"); + + request.setMitHistorie(true); + + SearchCriteriaType sct = new SearchCriteriaType(); + sct.setSucheUnternehmenNachIdRequest(request); + List<ResultRecord> results = searchByExample(sct, -1); + + return getURSearchResult(results); + } + + public List<URSearchResult> searchByBpk(String vzbpk) throws URClientException { + SucheUnternehmenNachBpkRequest request = new SucheUnternehmenNachBpkRequest(); + request.setVersion(version); + request.setBpkWTUREncoded(vzbpk); + + request.setNurAktive(true); + + SearchCriteriaType sct = new SearchCriteriaType(); + sct.setSucheUnternehmenNachBpkRequest(request); + + List<ResultRecord> results = searchByExample(sct, -1); + return getURSearchResult(results); + } + + public List<NichtNatuerlichePersonTyp> searchByRegisterNumber(String number, String type, int maxResults) throws URClientException { + SucheUnternehmenRequest request = new SucheUnternehmenRequest(); + request.setVersion(version); + SucheUnternehmensdaten sud = new SucheUnternehmensdaten(); + sud.setId(number); + sud.setIdArt(type); + request.setUnternehmensdaten(sud); + + + SearchCriteriaType sct = new SearchCriteriaType(); + sct.setSucheUnternehmenRequest(request); + + List<ResultRecord> results = searchByExample(sct, maxResults); + return getNichtNatuerlichePersonen(results); + } + + public List<NichtNatuerlichePersonTyp> searchByName(String name, String plz, int maxResults) throws URClientException { + SucheUnternehmenRequest request = new SucheUnternehmenRequest(); + request.setVersion(version); + SucheUnternehmensdaten sud = new SucheUnternehmensdaten(); + sud.setUntName(name); + if (MiscUtil.isNotEmpty(plz)) { + sud.setPlz(plz); + } + request.setUnternehmensdaten(sud); + + SearchCriteriaType sct = new SearchCriteriaType(); + sct.setSucheUnternehmenRequest(request); + + List<ResultRecord> results = searchByExample(sct, maxResults); + return getNichtNatuerlichePersonen(results); + } + + private List<URSearchResult> getURSearchResult(List<ResultRecord> records) throws URClientException { + + if (records == null || records.size() == 0) { + return new ArrayList<URSearchResult>(); + } + + List<URSearchResult> result = new ArrayList<URSearchResult>(); + + log.debug("UR SOAP response with " + records.size() + "records"); + + for (ResultRecord rr : records) { + PersonenDatenTyp pdt = rr.getPersonenDaten(); + NichtNatuerlichePersonTyp nnpt = pdt.getNichtNatuerlichePerson(); + Zusatzdaten zsd = pdt.getZusatzdaten(); + + //check validity era + boolean check_valera = isInValidityEra(zsd); + + //check allowed keys + boolean check_allowedkeys = hasAllowedKeys(nnpt.getIdentification()); + + //ERsB function codes + List<String> ersbcode = hasAllowedEVBCode(zsd); + + //check EVB + boolean check_EVB = isEVB(zsd); + + if (check_valera && check_allowedkeys && (check_EVB || !ersbcode.isEmpty())) { + result.add(new URSearchResult(nnpt, ersbcode, check_EVB)); + } + else { + log.debug("No validity for " + nnpt.getRest().get(0).getValue() + + "(Validity era=" + String.valueOf(check_valera) + + " Allowed Keys="+ String.valueOf(check_allowedkeys) + + " EVB check=" + String.valueOf(check_EVB) +")"); + } + } + + return result; + } + + private List<NichtNatuerlichePersonTyp> getNichtNatuerlichePersonen(List<ResultRecord> records) throws URClientException { + + if (records == null || records.size() == 0) { + return new ArrayList<NichtNatuerlichePersonTyp>(); + } + + List<NichtNatuerlichePersonTyp> result = new ArrayList<NichtNatuerlichePersonTyp>(); + + log.debug("UR SOAP response with " + records.size() + "records"); + + for (ResultRecord rr : records) { + PersonenDatenTyp pdt = rr.getPersonenDaten(); + NichtNatuerlichePersonTyp nnpt = pdt.getNichtNatuerlichePerson(); + Zusatzdaten zsd = pdt.getZusatzdaten(); + + //check validity era + boolean check_valera = isInValidityEra(zsd); + + //check allowed keys + boolean check_allowedkeys = hasAllowedKeys(nnpt.getIdentification()); + + //check EVB + boolean check_EVB = isEVB(zsd); + + if (check_valera && check_allowedkeys && check_EVB) { + result.add(nnpt); + } + else { + log.debug("No validity for " + nnpt.getRest().get(0).getValue() + + "(Validity era=" + String.valueOf(check_valera) + + " Allowed Keys="+ String.valueOf(check_allowedkeys) + + " EVB check=" + String.valueOf(check_EVB) +")"); + } + } + + return result; + } + + private List<String> hasAllowedEVBCode(Zusatzdaten zsd) { + + Set<String> set = new HashSet<String>(); + + if ((this.allowedERsBKeys == null || this.allowedERsBKeys.size() == 0)) { + return new ArrayList<String>(); + + } else { + + GregorianCalendar now = new GregorianCalendar(); + XMLGregorianCalendar xmldate; + try { + xmldate = DatatypeFactory.newInstance().newXMLGregorianCalendar(now); + if(zsd != null) { + List<PersonenDatenZusatzdatenTyp> pd = zsd.getPersonenDaten(); + for (PersonenDatenZusatzdatenTyp pdzd : pd) { + + ZusatzdatenPersonenTyp zusatzdaten = pdzd.getZusatzdaten(); + if (zusatzdaten != null) { + + List<FunktionVollzug> function = zusatzdaten.getFunktion(); + for (FunktionVollzug funct : function) { + + if (this.allowedERsBKeys.contains(String.valueOf(funct.getFktName()))) { + List<FktRegelTypVollzug> functRegel = funct.getFktRegel(); + for (FktRegelTypVollzug el : functRegel) { + if ( el.getFktRegelVon() == null && el.getFktRegelBis() == null) + set.add(String.valueOf(funct.getFktName())); + + else if ( el.getFktRegelVon() != null && el.getFktRegelBis() == null + && el.getFktRegelVon().compare(xmldate) == DatatypeConstants.LESSER ) + set.add(String.valueOf(funct.getFktName())); + + else if ( el.getFktRegelVon() == null && el.getFktRegelBis() != null + && el.getFktRegelBis().compare(xmldate) == DatatypeConstants.GREATER ) + set.add(String.valueOf(funct.getFktName())); + + + else if ( el.getFktRegelVon() != null && el.getFktRegelBis() != null + && (el.getFktRegelVon().compare(xmldate) == DatatypeConstants.LESSER + && el.getFktRegelBis().compare(xmldate) == DatatypeConstants.GREATER) ) + set.add(String.valueOf(funct.getFktName())); + + } + } + } + } + } + } + + } catch (DatatypeConfigurationException e) { + log.warn("Internal error during date conversion", e); + + } + + return new ArrayList<String>(set); + } + } + + //check if corporation is in validity era + //TODO: check correctness of TRUE if no validity era is defined!!! + private boolean isInValidityEra(Zusatzdaten zsd) throws URClientException { + if(zsd != null) { + List<BestandszeitraumVollzug> validityera = zsd.getBestandszeitraum(); + for ( BestandszeitraumVollzug valera_el : validityera) { + try { + GregorianCalendar now = new GregorianCalendar(); + XMLGregorianCalendar xmldate = DatatypeFactory.newInstance().newXMLGregorianCalendar(now); + + if ( !(valera_el.getBestandVon().compare(xmldate) == DatatypeConstants.LESSER + && valera_el.getBestandBis().compare(xmldate) == DatatypeConstants.GREATER) ) { + + log.debug("Corporation is not within the validity period"); + return false; + } + + } catch (DatatypeConfigurationException e) { + log.warn("Internal error during date conversion", e); + } + } + } + return true; + } + + // check if person is einzelvertretungsbefugt + private boolean isEVB(Zusatzdaten zsd) { + if (!this.evbCheck) { + return true; + } + else { + if(zsd != null) { + List<PersonenDatenZusatzdatenTyp> pd = zsd.getPersonenDaten(); + for (PersonenDatenZusatzdatenTyp pdzd : pd) { + + if (pdzd.getZusatzdaten() != null) { + EvbStatusTyp evbstatus = pdzd.getZusatzdaten().getEvbStatus(); + if (evbstatus!=null && evbstatus.equals(EvbStatusTyp.J)) { + return true; + } + } + } + } + } + return false; + } + + //check allowedKeys + private boolean hasAllowedKeys(List<IdentificationType> list) throws URClientException { + + if ((this.allowedKeys == null || this.allowedKeys.size() == 0)) { + return true; + + } else { + for (IdentificationType idt : list) { + if (this.allowedKeys.contains(idt.getType())) { + return true; + } + } + } + return false; + } + + + + // search by example + public List<ResultRecord> searchByExample(SearchCriteriaType sct, int maxResults) throws URClientException { + + + SearchByExampleType body = new SearchByExampleType(); + + body.setSearchRequestId(createURSearchId()); + ResultCriteriaType rc = new ResultCriteriaType(); + + log.debug("UR SOAP request with Id " + body.getSearchRequestId()); + + // set only value of max results if > 0 + if (maxResults > 0) { + rc.setMaxRecords(BigInteger.valueOf(maxResults)); + } + + // set ExtraResultCriterias + ExtraResultCriteria ec = new ExtraResultCriteria(); + ec.setErsbAttribute(true); + rc.setExtraResultCriteria(ec); + body.setResultCriteria(rc); + + body.setSearchCriteria(sct); + + + //request UR service + SearchResponseType srt = null; + try { + srt = this.urSuche.searchByExample(body); + + } catch (javax.xml.ws.soap.SOAPFaultException e) { + //request secondary service + switchTOSecondarySystem(e); + try { + srt = this.urSuche.searchByExample(body); + + } catch (XmlSwFault e1) { + + if (e1.getFaultInfo() != null) { + throw new URClientException(e1.getFaultInfo().getHelp(), e1.getFaultInfo().getCode().intValue()); + + } else { + throw new URClientException(e1.getMessage(), -1, e1); + } + } + + } catch (WebServiceException e) { + //request secondary service + switchTOSecondarySystem(e); + + try { + srt = this.urSuche.searchByExample(body); + + } catch (XmlSwFault e1) { + + if (e1.getFaultInfo() != null) { + throw new URClientException(e1.getFaultInfo().getHelp(), e1.getFaultInfo().getCode().intValue()); + + } else { + throw new URClientException(e1.getMessage(), -1, e1); + } + } + + } catch (XmlSwFault e) { + + if (e.getFaultInfo() != null) { + throw new URClientException(e.getFaultInfo().getHelp(), e.getFaultInfo().getCode().intValue()); + + } else { + throw new URClientException(e.getMessage(), -1, e); + } + + } + + // 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<ResultRecord>(); + } + return srt.getResultRecords().getResultRecord(); + } + + private void switchTOSecondarySystem(Exception e) throws URClientException { + log.warn("Request primary UR service failed with error \"" + e.getMessage() +"\". " + + "Switch to secondary UR service."); + + String urURL = config.getURSecondaryProductionEnvironmentURL(); + try { + setURServiceURL(urURL); + + } catch (EgovUtilException e1) { + log.error("Switch to secodary UR service FAILED!", e1); + throw new URClientException(e1.getMessage(), 0); + } + } + + private void setURServiceURL(String urURL) throws EgovUtilException { + // set UR Service URL + + log.trace("UR connection URL: " + urURL); + BindingProvider bindingProvider = (BindingProvider) urSuche; + Map<String, Object> requestContext = bindingProvider.getRequestContext(); + requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, urURL); + + //set HTTP Client Timeout (Default Timeout 60sec) + Client client = ClientProxy.getClient(urSuche); + HTTPConduit http = (HTTPConduit) client.getConduit(); + HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy(); + + //set HTTP Client Timeout (Default Timeout 60sec) + httpClientPolicy.setConnectionTimeout(15000); + httpClientPolicy.setAllowChunking(false); + httpClientPolicy.setReceiveTimeout(32000); + http.setClient(httpClientPolicy); + + //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."); + } + TLSClientParameters tlsParams = new TLSClientParameters(); + tlsParams.setSSLSocketFactory(sslContext.getSocketFactory()); + + // check for lax hostname + if (this.config.getURsslConfiguration().useLaxHostNameVerifier()) { + log.trace("LaxHostnameVerifier enabled. This setting is not recommended to use."); + tlsParams.setHostnameVerifier(new LaxHostNameVerifier()); + } + http.setTlsClientParameters(tlsParams ); + } + + } + + + @SuppressWarnings({ "rawtypes", "unchecked" }) + private void initialize() throws EgovUtilException { + URL url = URClient.class.getResource("/wsdl/ur_V5/URSucheService.wsdl"); + URSucheService urService = null; + urService = new URSucheService(url, new QName("urn:at:statistik:ur:ws", "URSucheService")); + urSuche = urService.getURSucheService(); + + BindingProvider bindingProvider = (BindingProvider) urSuche; + + //set service URLs + String urURL = null; + if (config.isURTestEnvironment()) { + log.trace("Initializing UR test configuration."); + urURL = config.getURTestEnvironmentURL(); + } else { + log.trace("Initializing UR productive configuration."); + urURL = config.getURProductionEnvironmentURL(); + } + + setURServiceURL(urURL); + + log.trace("Adding JAX-WS request/response trace handler."); + List<Handler> 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(); + Pvp18pSoapHandler szrSOAPHandler = new Pvp18pSoapHandler(); + szrSOAPHandler.configure(pvpToken); + handlerList.add(szrSOAPHandler); + bindingProvider.getBinding().setHandlerChain(handlerList); + } + + private String createURSearchId() { + return RandomStringUtils.randomAlphanumeric(8) + "-" + + RandomStringUtils.randomAlphanumeric(4) + "-" + RandomStringUtils.randomAlphanumeric(4) + "-" + + RandomStringUtils.randomAlphanumeric(12); + } + + public class URSearchResult { + private NichtNatuerlichePersonTyp result; + private List<String> ersbcode; + private boolean evb; + + public URSearchResult() { + this.result = null; + this.ersbcode = null; + this.evb = false; + } + + public URSearchResult(NichtNatuerlichePersonTyp result, List<String> ersb_code, boolean isevb) { + this.result = result; + this.ersbcode = ersb_code; + this.evb = isevb; + } + + public NichtNatuerlichePersonTyp getResult() { + return result; + } + + public List<String> getErsbcode() { + return ersbcode; + } + + public boolean isEVB() { + return evb; + } + } + + +} + diff --git a/src/main/java/at/gv/util/client/ur_V7/URClientException.java b/src/main/java/at/gv/util/client/ur_V7/URClientException.java new file mode 100644 index 0000000..fa922b9 --- /dev/null +++ b/src/main/java/at/gv/util/client/ur_V7/URClientException.java @@ -0,0 +1,33 @@ +package at.gv.util.client.ur_V7; + +public class URClientException extends Exception { + + private static final long serialVersionUID = 5173056345209288701L; + + private int code = -1; + + public URClientException(int code) { + super(); + this.code = code; + } + + public URClientException(String message, int code, Throwable cause) { + super(message, cause); + this.code = code; + } + + public URClientException(String message, int code) { + super(message); + this.code = code; + } + + public URClientException(int code, Throwable cause) { + super(cause); + this.code = code; + } + + public int getCode() { + return this.code; + } + +} |