You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
155 lines
4.7 KiB
155 lines
4.7 KiB
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");
|
|
}
|
|
}
|
|
}
|
|
|