I am using jsoup to download excel file from google drive via Java.
I need to connect to google drive via http proxy in office, and can connect to google drive without proxy at home.
How to use Spring profile to configure to fit my different working environment?
How-To
1. Create a DownloadService Interface
package com.cht.tool.filegenerator.service; import java.io.IOException; public interface DownloadService { void downloadExcel() throws IOException; }
2. Create a JSoupDefaultService serivce and mark with default profile
package com.cht.tool.filegenerator.service; import lombok.extern.slf4j.Slf4j; import org.jsoup.Connection; import org.jsoup.Jsoup; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Service; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; @Slf4j @Service @Profile({"default"}) public class JSoupDefaultService implements DownloadService { @Value("${jsoup.from}") private String from; @Value("${jsoup.to}") private String to; public void downloadExcel() throws IOException { log.debug("default profile"); Connection.Response response = null; try { response = Jsoup.connect(from) .ignoreContentType(true) .execute(); } catch (IOException e) { throw new IOException("fail to connect to google drive, error : " + e.getMessage(), e); } log.debug("status code = {}", response.statusCode()); Files.write(Paths.get(to), response.bodyAsBytes()); log.debug("excel file downloaded."); } }
3. Create a JSoupService service and mark with office profile
package com.cht.tool.filegenerator.service; import lombok.extern.slf4j.Slf4j; import org.jsoup.Connection; import org.jsoup.Jsoup; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Service; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; @Slf4j @Service @Profile("office") public class JSoupService implements DownloadService { @Value("${jsoup.from}") private String from; @Value("${jsoup.to}") private String to; @Value("${jsoup.proxy}") private String proxy; @Value("${jsoup.port}") private int port; public void downloadExcel() throws IOException { log.debug("office profile"); Connection.Response response = null; try { response = Jsoup.connect(from) .proxy(proxy, port) .ignoreContentType(true) .execute(); } catch (IOException e) { throw new IOException("fail to connect to google drive, error : " + e.getMessage(), e); } log.debug("status code = {}", response.statusCode()); Files.write(Paths.get(to), response.bodyAsBytes()); log.debug("excel file downloaded."); } }
4. Create a default profile in yaml
spring: profiles: active: default
5. Write Test code in Spring Application
package com.cht.tool.filegenerator; import com.cht.tool.filegenerator.service.DownloadService; import com.cht.tool.filegenerator.service.FileGeneratorService; import com.cht.tool.filegenerator.service.JSoupService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.scheduling.annotation.EnableAsync; import java.nio.file.Files; import java.nio.file.Paths; @Slf4j @SpringBootApplication public class FileGeneratorApplication { @Value("${jsoup.to}") private String to; public static void main(String[] args) { SpringApplication.run(FileGeneratorApplication.class, args); } @Bean CommandLineRunner run(FileGeneratorService fileGeneratorService, DownloadService downloadService) { return args -> { if (Files.exists(Paths.get(to))) { Files.delete(Paths.get(to)); } downloadService.downloadExcel(); fileGeneratorService.writePropertyFile(); fileGeneratorService.writeFtpPropertyFile(); fileGeneratorService.writeFtlFile(); fileGeneratorService.writeSqlFile(); }; } }
6. Do test
2020-07-24 11:18:50.529 DEBUG 6628 --- [ main] c.c.t.f.FileGeneratorApplication : Running with Spring Boot v2.3.1.RELEASE, Spring v5.2.7.RELEASE 2020-07-24 11:18:50.529 INFO 6628 --- [ main] c.c.t.f.FileGeneratorApplication : The following profiles are active: default 2020-07-24 11:18:51.328 INFO 6628 --- [ main] c.c.t.f.FileGeneratorApplication : Started FileGeneratorApplication in 1.355 seconds (JVM running for 2.489) 2020-07-24 11:18:51.343 DEBUG 6628 --- [ main] c.c.t.f.service.JSoupDefaultService : default profile 2020-07-24 11:18:53.501 DEBUG 6628 --- [ main] c.c.t.f.service.JSoupDefaultService : status code = 200 2020-07-24 11:18:53.732 DEBUG 6628 --- [ main] c.c.t.f.service.JSoupDefaultService : excel file downloaded.
No comments:
Post a Comment