parent
947c13db1a
commit
7aa3e4692b
@ -0,0 +1,50 @@ |
||||
package me.bearns.fias.service; |
||||
|
||||
import lombok.AllArgsConstructor; |
||||
import me.bearns.fias.domain.FiasVersion; |
||||
import org.springframework.web.client.RestTemplate; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Arrays; |
||||
import java.util.List; |
||||
import java.util.stream.Collectors; |
||||
|
||||
@AllArgsConstructor |
||||
public class OnlineVersionImpl implements OnlineVersion{ |
||||
|
||||
private static final String GET_ALL_FILE_INFO = "http://fias.nalog.ru/WebServices/Public/GetAllDownloadFileInfo"; |
||||
private static final String GET_LAST_FILE_INFO = "http://fias.nalog.ru/WebServices/Public/GetLastDownloadFileInfo"; |
||||
|
||||
RestTemplate client; |
||||
|
||||
@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"); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue