Compare commits
4 Commits
694bafcd98
...
24f177cf15
Author | SHA1 | Date |
---|---|---|
![]() |
24f177cf15 | 5 years ago |
![]() |
ba645884d6 | 5 years ago |
![]() |
7aa3e4692b | 5 years ago |
![]() |
947c13db1a | 5 years ago |
@ -0,0 +1,52 @@ |
|||||||
|
package me.bearns.fias.service; |
||||||
|
|
||||||
|
import lombok.AllArgsConstructor; |
||||||
|
import me.bearns.fias.domain.FiasVersion; |
||||||
|
import org.springframework.stereotype.Component; |
||||||
|
import org.springframework.web.client.RestTemplate; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.Arrays; |
||||||
|
import java.util.List; |
||||||
|
import java.util.stream.Collectors; |
||||||
|
|
||||||
|
//@AllArgsConstructor
|
||||||
|
@Component |
||||||
|
public class OnlineVersionImpl implements OnlineVersion{ |
||||||
|
|
||||||
|
private static final String GET_ALL_FILE_INFO = "https://fias.nalog.ru/WebServices/Public/GetAllDownloadFileInfo"; |
||||||
|
private static final String GET_LAST_FILE_INFO = "https://fias.nalog.ru/WebServices/Public/GetLastDownloadFileInfo"; |
||||||
|
|
||||||
|
RestTemplate client = new RestTemplate(); |
||||||
|
|
||||||
|
@Override |
||||||
|
public long getLastVersionId() { |
||||||
|
|
||||||
|
final FiasVersion version = client.getForObject(GET_LAST_FILE_INFO, FiasVersion.class); |
||||||
|
|
||||||
|
if(version != null) return version.getVersionId(); |
||||||
|
|
||||||
|
return 0; //todo
|
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public List<FiasVersion> getVersionsAfter(long version) { |
||||||
|
|
||||||
|
final FiasVersion[] versions = client.getForObject(GET_ALL_FILE_INFO, FiasVersion[].class); |
||||||
|
|
||||||
|
if(versions != null) { |
||||||
|
return Arrays.stream(versions).filter(v -> v.getVersionId() > version).collect(Collectors.toList()); |
||||||
|
} |
||||||
|
|
||||||
|
return null; //todo
|
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public List<FiasVersion> getLastVersion() { |
||||||
|
List<FiasVersion> versions = new ArrayList<>(); |
||||||
|
|
||||||
|
versions.add(client.getForObject(GET_LAST_FILE_INFO, FiasVersion.class)); |
||||||
|
|
||||||
|
return versions; //todo
|
||||||
|
} |
||||||
|
} |
@ -1,155 +0,0 @@ |
|||||||
package me.bearns.fias.service; |
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j; |
|
||||||
import me.bearns.fias.domain.FiasVersion; |
|
||||||
import org.springframework.stereotype.Component; |
|
||||||
import ru.nalog.fias.ArrayOfDownloadFileInfo; |
|
||||||
import ru.nalog.fias.DownloadFileInfo; |
|
||||||
import ru.nalog.fias.IDownloadService; |
|
||||||
import ru.nalog.fias.IDownloadService_Service; |
|
||||||
import sun.net.www.protocol.http.HttpURLConnection; |
|
||||||
|
|
||||||
import javax.xml.ws.BindingProvider; |
|
||||||
import java.io.File; |
|
||||||
import java.io.InputStream; |
|
||||||
import java.net.URL; |
|
||||||
import java.nio.file.Files; |
|
||||||
import java.nio.file.StandardCopyOption; |
|
||||||
import java.util.ArrayList; |
|
||||||
import java.util.List; |
|
||||||
import java.util.Map; |
|
||||||
import java.util.stream.Collectors; |
|
||||||
|
|
||||||
import static javax.xml.ws.BindingProvider.ENDPOINT_ADDRESS_PROPERTY; |
|
||||||
|
|
||||||
@Slf4j |
|
||||||
@Component |
|
||||||
public class SOAPClient implements OnlineVersion { |
|
||||||
|
|
||||||
private final static String RESOURCE_WSDL = "/DownloadService.wsdl"; |
|
||||||
|
|
||||||
IDownloadService downloadService; |
|
||||||
|
|
||||||
public SOAPClient() { |
|
||||||
|
|
||||||
log.info("Init SOAP client"); |
|
||||||
|
|
||||||
try (InputStream stream = getClass().getResourceAsStream(RESOURCE_WSDL)) { |
|
||||||
|
|
||||||
final File tempFile = File.createTempFile(RESOURCE_WSDL, "tmp"); |
|
||||||
|
|
||||||
tempFile.deleteOnExit(); |
|
||||||
|
|
||||||
if(Files.copy(stream, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING) > 0) { |
|
||||||
|
|
||||||
//copy some bytes ok
|
|
||||||
log.debug("WSDL saved to tmp file, try to init service"); |
|
||||||
|
|
||||||
//Init soap client
|
|
||||||
IDownloadService_Service service = new IDownloadService_Service(tempFile.toURI().toURL()); |
|
||||||
|
|
||||||
downloadService = service.getBasicHttpBindingIDownloadService(); |
|
||||||
|
|
||||||
log.debug("Service ok, enable redirect hook"); |
|
||||||
|
|
||||||
setRedirect(downloadService); |
|
||||||
|
|
||||||
log.info("SOAP client init completed"); |
|
||||||
} |
|
||||||
|
|
||||||
}catch (Exception e) { |
|
||||||
log.error("Caught exception while init SOAP client"); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public long getLastVersionId() { |
|
||||||
|
|
||||||
FiasVersion last = getLast(); |
|
||||||
if(last != null) return last.getVersionId(); |
|
||||||
|
|
||||||
return 0; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public List<FiasVersion> getVersionsAfter(long version) { |
|
||||||
|
|
||||||
final ArrayOfDownloadFileInfo allDownloadFileInfo = downloadService.getAllDownloadFileInfo(); |
|
||||||
|
|
||||||
if(allDownloadFileInfo != null) { |
|
||||||
|
|
||||||
final List<DownloadFileInfo> fileInfos = allDownloadFileInfo.getDownloadFileInfo(); |
|
||||||
|
|
||||||
if(fileInfos != null) { |
|
||||||
return fileInfos.stream() |
|
||||||
.filter(f -> f.getVersionId() > version) |
|
||||||
.map(f -> new FiasVersion(f.getVersionId(),f.getTextVersion(),f.getFiasCompleteXmlUrl(),f.getFiasDeltaXmlUrl())) |
|
||||||
.collect(Collectors.toList()); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
return null; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public List<FiasVersion> getLastVersion() { |
|
||||||
|
|
||||||
final FiasVersion last = getLast(); |
|
||||||
|
|
||||||
if(last != null) { |
|
||||||
|
|
||||||
final ArrayList<FiasVersion> list = new ArrayList<>(1); |
|
||||||
list.add(last); |
|
||||||
|
|
||||||
return list; |
|
||||||
} |
|
||||||
|
|
||||||
return null; |
|
||||||
} |
|
||||||
|
|
||||||
private FiasVersion getLast() { |
|
||||||
|
|
||||||
DownloadFileInfo fileInfo = downloadService.getLastDownloadFileInfo(); |
|
||||||
|
|
||||||
if(fileInfo !=null) { |
|
||||||
|
|
||||||
//TODO check null in getVersionId()
|
|
||||||
|
|
||||||
return new FiasVersion(fileInfo.getVersionId(), fileInfo.getTextVersion(), fileInfo.getFiasCompleteXmlUrl(), fileInfo.getFiasDeltaXmlUrl()); |
|
||||||
} |
|
||||||
|
|
||||||
return null; |
|
||||||
} |
|
||||||
|
|
||||||
private static void setRedirect(final Object bindingProvider) { |
|
||||||
try { |
|
||||||
if(bindingProvider instanceof BindingProvider) { |
|
||||||
|
|
||||||
Map<String, Object> requestContext = ((BindingProvider) bindingProvider).getRequestContext(); |
|
||||||
|
|
||||||
Object endpoint = requestContext.get(ENDPOINT_ADDRESS_PROPERTY); |
|
||||||
|
|
||||||
if(endpoint instanceof String) { |
|
||||||
|
|
||||||
final URL url = new URL((String) endpoint); |
|
||||||
|
|
||||||
final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
|
||||||
connection.setInstanceFollowRedirects(true); |
|
||||||
connection.setRequestMethod("POST"); |
|
||||||
connection.setRequestProperty("Content-Type", "text/html; charset='UTF-8'"); |
|
||||||
connection.setDoOutput(true); |
|
||||||
|
|
||||||
final int responseCode = connection.getResponseCode(); |
|
||||||
|
|
||||||
if (responseCode >= 300 && responseCode < 400) { |
|
||||||
final String redirect = connection.getHeaderField("location"); |
|
||||||
|
|
||||||
requestContext.put(ENDPOINT_ADDRESS_PROPERTY, redirect); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
} catch(final Exception e) { |
|
||||||
log.error("Endpoint redirect not set"); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,46 @@ |
|||||||
|
package me.bearns.fias.xml; |
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j; |
||||||
|
|
||||||
|
import javax.xml.bind.ValidationEventHandler; |
||||||
|
import javax.xml.bind.annotation.adapters.XmlAdapter; |
||||||
|
import java.math.BigInteger; |
||||||
|
|
||||||
|
@Slf4j |
||||||
|
public class DecimalAdapter extends XmlAdapter<String, BigInteger> { |
||||||
|
|
||||||
|
/** |
||||||
|
* Convert a value type to a bound type. |
||||||
|
* |
||||||
|
* @param v The value to be converted. Can be null. |
||||||
|
* @throws Exception if there's an error during the conversion. The caller is responsible for |
||||||
|
* reporting the error to the user through {@link ValidationEventHandler}. |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public BigInteger unmarshal(String v) throws Exception { |
||||||
|
if (v == null) return null; |
||||||
|
if (v.isEmpty()) { |
||||||
|
log.trace("BigInteger bug bypassed"); |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
if (!v.matches("^\\d+$")) return null; |
||||||
|
|
||||||
|
return new BigInteger(v); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Convert a bound type to a value type. |
||||||
|
* |
||||||
|
* @param v The value to be convereted. Can be null. |
||||||
|
* @throws Exception if there's an error during the conversion. The caller is responsible for |
||||||
|
* reporting the error to the user through {@link ValidationEventHandler}. |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public String marshal(BigInteger v) throws Exception { |
||||||
|
//TODO BigInteger marshaling impl
|
||||||
|
//throw new Exception("Marshalling not implemented");
|
||||||
|
|
||||||
|
return v == null ? null : v.toString(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,59 @@ |
|||||||
|
package me.bearns.fias; |
||||||
|
|
||||||
|
import me.bearns.fias.domain.FiasVersion; |
||||||
|
import me.bearns.fias.service.OnlineVersion; |
||||||
|
import org.junit.Ignore; |
||||||
|
import org.junit.runner.RunWith; |
||||||
|
import org.junit.Test; |
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.boot.test.context.SpringBootTest; |
||||||
|
import org.springframework.test.context.ActiveProfiles; |
||||||
|
import org.springframework.test.context.junit4.SpringRunner; |
||||||
|
|
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
@ActiveProfiles("test") |
||||||
|
@RunWith(SpringRunner.class) |
||||||
|
@SpringBootTest |
||||||
|
public class OnlineVersionsTest { |
||||||
|
@Autowired |
||||||
|
OnlineVersion client; |
||||||
|
|
||||||
|
@Ignore |
||||||
|
@Test |
||||||
|
public void getLastTest(){ |
||||||
|
final long lastVersionId = client.getLastVersionId(); |
||||||
|
|
||||||
|
assert lastVersionId > 0; |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void getAfterTest(){ |
||||||
|
|
||||||
|
final long count = 14; |
||||||
|
|
||||||
|
final long lastVersionId = client.getLastVersionId(); |
||||||
|
|
||||||
|
assert lastVersionId > 0; |
||||||
|
|
||||||
|
final long fromVersion = lastVersionId - count; |
||||||
|
|
||||||
|
final List<FiasVersion> versions = client.getVersionsAfter(fromVersion); |
||||||
|
|
||||||
|
assert versions != null; |
||||||
|
|
||||||
|
assert versions.size() <= count; |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void getLastObjTest(){ |
||||||
|
final List<FiasVersion> versions = client.getLastVersion(); |
||||||
|
|
||||||
|
assert versions != null; |
||||||
|
|
||||||
|
assert versions.size() == 1; |
||||||
|
|
||||||
|
assert versions.get(0) != null; |
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue