20
20
import static org .apache .commons .lang3 .StringUtils .isNotEmpty ;
21
21
import static org .openapitools .codegen .config .CodegenConfiguratorUtils .*;
22
22
23
+ import io .swagger .v3 .parser .core .models .AuthorizationValue ;
23
24
import java .io .File ;
25
+ import java .io .FileOutputStream ;
26
+ import java .io .IOException ;
27
+ import java .net .MalformedURLException ;
28
+ import java .net .URI ;
29
+ import java .net .URISyntaxException ;
30
+ import java .net .URL ;
31
+ import java .net .URLConnection ;
32
+ import java .nio .channels .Channels ;
33
+ import java .nio .channels .FileChannel ;
34
+ import java .nio .channels .ReadableByteChannel ;
24
35
import java .util .HashMap ;
25
36
import java .util .List ;
26
37
import java .util .Map ;
42
53
import org .openapitools .codegen .CodegenConfig ;
43
54
import org .openapitools .codegen .CodegenConstants ;
44
55
import org .openapitools .codegen .DefaultGenerator ;
56
+ import org .openapitools .codegen .auth .AuthParser ;
45
57
import org .openapitools .codegen .config .CodegenConfigurator ;
46
58
import org .openapitools .codegen .config .GlobalSettings ;
47
59
import org .sonatype .plexus .build .incremental .BuildContext ;
@@ -429,7 +441,7 @@ public void execute() throws MojoExecutionException {
429
441
if (inputSpecFile .exists ()) {
430
442
File storedInputSpecHashFile = getHashFile (inputSpecFile );
431
443
if (storedInputSpecHashFile .exists ()) {
432
- String inputSpecHash = Files . asByteSource (inputSpecFile ). hash ( Hashing . sha256 ()). toString ( );
444
+ String inputSpecHash = calculateInputSpecHash (inputSpecFile );
433
445
String storedInputSpecHash = Files .asCharSource (storedInputSpecHashFile , Charsets .UTF_8 ).read ();
434
446
if (inputSpecHash .equals (storedInputSpecHash )) {
435
447
getLog ().info (
@@ -720,12 +732,7 @@ public void execute() throws MojoExecutionException {
720
732
721
733
// Store a checksum of the input spec
722
734
File storedInputSpecHashFile = getHashFile (inputSpecFile );
723
- ByteSource inputSpecByteSource =
724
- inputSpecFile .exists ()
725
- ? Files .asByteSource (inputSpecFile )
726
- : CharSource .wrap (ClasspathHelper .loadFileFromClasspath (inputSpecFile .toString ().replaceAll ("\\ \\ " ,"/" )))
727
- .asByteSource (Charsets .UTF_8 );
728
- String inputSpecHash =inputSpecByteSource .hash (Hashing .sha256 ()).toString ();
735
+ String inputSpecHash = calculateInputSpecHash (inputSpecFile );
729
736
730
737
if (storedInputSpecHashFile .getParent () != null && !new File (storedInputSpecHashFile .getParent ()).exists ()) {
731
738
File parent = new File (storedInputSpecHashFile .getParent ());
@@ -746,8 +753,75 @@ public void execute() throws MojoExecutionException {
746
753
}
747
754
}
748
755
756
+ /**
757
+ * Calculate openapi specification file hash. If specification is hosted on remote resource it is downloaded first
758
+ *
759
+ * @param inputSpecFile - Openapi specification input file to calculate it's hash.
760
+ * Does not taken into account if input spec is hosted on remote resource
761
+ * @return openapi specification file hash
762
+ * @throws IOException
763
+ */
764
+ private String calculateInputSpecHash (File inputSpecFile ) throws IOException {
765
+
766
+ URL inputSpecRemoteUrl = inputSpecRemoteUrl ();
767
+
768
+ File inputSpecTempFile = inputSpecFile ;
769
+
770
+ if (inputSpecRemoteUrl != null ) {
771
+ inputSpecTempFile = File .createTempFile ("openapi-spec" , ".tmp" );
772
+
773
+ URLConnection conn = inputSpecRemoteUrl .openConnection ();
774
+ if (isNotEmpty (auth )) {
775
+ List <AuthorizationValue > authList = AuthParser .parse (auth );
776
+ for (AuthorizationValue auth : authList ) {
777
+ conn .setRequestProperty (auth .getKeyName (), auth .getValue ());
778
+ }
779
+ }
780
+ ReadableByteChannel readableByteChannel = Channels .newChannel (conn .getInputStream ());
781
+
782
+ FileOutputStream fileOutputStream = new FileOutputStream (inputSpecTempFile );
783
+ FileChannel fileChannel = fileOutputStream .getChannel ();
784
+
785
+ fileChannel .transferFrom (readableByteChannel , 0 , Long .MAX_VALUE );
786
+ }
787
+
788
+ ByteSource inputSpecByteSource =
789
+ inputSpecTempFile .exists ()
790
+ ? Files .asByteSource (inputSpecTempFile )
791
+ : CharSource .wrap (ClasspathHelper .loadFileFromClasspath (inputSpecTempFile .toString ().replaceAll ("\\ \\ " ,"/" )))
792
+ .asByteSource (Charsets .UTF_8 );
793
+
794
+ return inputSpecByteSource .hash (Hashing .sha256 ()).toString ();
795
+ }
796
+
797
+ /**
798
+ * Try to parse inputSpec setting string into URL
799
+ * @return A valid URL or null if inputSpec is not a valid URL
800
+ */
801
+ private URL inputSpecRemoteUrl (){
802
+ try {
803
+ return new URI (inputSpec ).toURL ();
804
+ } catch (URISyntaxException | MalformedURLException | IllegalArgumentException e ) {
805
+ return null ;
806
+ }
807
+ }
808
+
809
+ /**
810
+ * Get specification hash file
811
+ * @param inputSpecFile - Openapi specification input file to calculate it's hash.
812
+ * Does not taken into account if input spec is hosted on remote resource
813
+ * @return a file with previously calculated hash
814
+ */
749
815
private File getHashFile (File inputSpecFile ) {
750
- return new File (output .getPath () + File .separator + ".openapi-generator" + File .separator + inputSpecFile .getName () + ".sha256" );
816
+ String name = inputSpecFile .getName ();
817
+
818
+ URL url = inputSpecRemoteUrl ();
819
+ if (url != null ) {
820
+ String [] segments = url .getPath ().split ("/" );
821
+ name = Files .getNameWithoutExtension (segments [segments .length - 1 ]);
822
+ }
823
+
824
+ return new File (output .getPath () + File .separator + ".openapi-generator" + File .separator + name + ".sha256" );
751
825
}
752
826
753
827
private String getCompileSourceRoot () {
@@ -757,8 +831,7 @@ private String getCompileSourceRoot() {
757
831
final String sourceFolder =
758
832
sourceFolderObject == null ? "src/main/java" : sourceFolderObject .toString ();
759
833
760
- String sourceJavaFolder = output .toString () + "/" + sourceFolder ;
761
- return sourceJavaFolder ;
834
+ return output .toString () + "/" + sourceFolder ;
762
835
}
763
836
764
837
private void addCompileSourceRootIfConfigured () {
@@ -803,4 +876,4 @@ private void adjustAdditionalProperties(final CodegenConfig config) {
803
876
}
804
877
}
805
878
}
806
- }
879
+ }
0 commit comments