Json rest API (untested)
This commit is contained in:
@@ -72,32 +72,6 @@
|
|||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.cxf</groupId>
|
|
||||||
<artifactId>cxf-codegen-plugin</artifactId>
|
|
||||||
<version>3.3.6</version>
|
|
||||||
<executions>
|
|
||||||
<execution>
|
|
||||||
<id>generate-sources</id>
|
|
||||||
<phase>generate-sources</phase>
|
|
||||||
<configuration>
|
|
||||||
<sourceRoot>${project.build.directory}/generated-sources/cxf</sourceRoot>
|
|
||||||
<wsdlOptions>
|
|
||||||
<wsdlOption>
|
|
||||||
<extraargs>
|
|
||||||
<extraarg>-p</extraarg>
|
|
||||||
<extraarg>ru.nalog.fias</extraarg>
|
|
||||||
</extraargs>
|
|
||||||
<wsdl>${basedir}/src/main/resources/DownloadService.wsdl</wsdl>
|
|
||||||
</wsdlOption>
|
|
||||||
</wsdlOptions>
|
|
||||||
</configuration>
|
|
||||||
<goals>
|
|
||||||
<goal>wsdl2java</goal>
|
|
||||||
</goals>
|
|
||||||
</execution>
|
|
||||||
</executions>
|
|
||||||
</plugin>
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package me.bearns.fias.domain;
|
package me.bearns.fias.domain;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonAlias;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
|
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
@@ -8,6 +10,7 @@ import javax.persistence.Entity;
|
|||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
|
|
||||||
@ToString
|
@ToString
|
||||||
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "version")
|
@Table(name = "version")
|
||||||
public class FiasVersion {
|
public class FiasVersion {
|
||||||
@@ -24,15 +27,19 @@ public class FiasVersion {
|
|||||||
|
|
||||||
@Id
|
@Id
|
||||||
@Column(name = "version_id")
|
@Column(name = "version_id")
|
||||||
|
@JsonAlias("VersionId")
|
||||||
protected Long versionId;
|
protected Long versionId;
|
||||||
|
|
||||||
@Column(name = "version_txt")
|
@Column(name = "version_txt")
|
||||||
|
@JsonAlias("TextVersion")
|
||||||
protected String textVersion;
|
protected String textVersion;
|
||||||
|
|
||||||
@Column(name = "xml_url")
|
@Column(name = "xml_url")
|
||||||
|
@JsonAlias("FiasCompleteXmlUrl")
|
||||||
protected String fiasCompleteXmlUrl;
|
protected String fiasCompleteXmlUrl;
|
||||||
|
|
||||||
@Column(name = "delta_xml_url")
|
@Column(name = "delta_xml_url")
|
||||||
|
@JsonAlias("FiasDeltaXmlUrl")
|
||||||
protected String fiasDeltaXmlUrl;
|
protected String fiasDeltaXmlUrl;
|
||||||
|
|
||||||
@Column(name = "regions")
|
@Column(name = "regions")
|
||||||
|
|||||||
@@ -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");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user