21
21
import static org .openapitools .codegen .config .CodegenConfiguratorUtils .*;
22
22
23
23
import java .io .File ;
24
+ import java .io .FileOutputStream ;
25
+ import java .io .IOException ;
26
+ import java .net .MalformedURLException ;
27
+ import java .net .URI ;
28
+ import java .net .URISyntaxException ;
29
+ import java .net .URL ;
30
+ import java .nio .channels .Channels ;
31
+ import java .nio .channels .FileChannel ;
32
+ import java .nio .channels .ReadableByteChannel ;
24
33
import java .util .HashMap ;
25
34
import java .util .List ;
26
35
import java .util .Map ;
@@ -429,7 +438,7 @@ public void execute() throws MojoExecutionException {
429
438
if (inputSpecFile .exists ()) {
430
439
File storedInputSpecHashFile = getHashFile (inputSpecFile );
431
440
if (storedInputSpecHashFile .exists ()) {
432
- String inputSpecHash = Files . asByteSource (inputSpecFile ). hash ( Hashing . sha256 ()). toString ( );
441
+ String inputSpecHash = calculateInputSpecHash (inputSpecFile );
433
442
String storedInputSpecHash = Files .asCharSource (storedInputSpecHashFile , Charsets .UTF_8 ).read ();
434
443
if (inputSpecHash .equals (storedInputSpecHash )) {
435
444
getLog ().info (
@@ -720,12 +729,7 @@ public void execute() throws MojoExecutionException {
720
729
721
730
// Store a checksum of the input spec
722
731
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 ();
732
+ String inputSpecHash = calculateInputSpecHash (inputSpecFile );
729
733
730
734
if (storedInputSpecHashFile .getParent () != null && !new File (storedInputSpecHashFile .getParent ()).exists ()) {
731
735
File parent = new File (storedInputSpecHashFile .getParent ());
@@ -746,8 +750,70 @@ public void execute() throws MojoExecutionException {
746
750
}
747
751
}
748
752
753
+ /**
754
+ * Calculate openapi specification file hash. If specification is hosted on remote resource it is downloaded first
755
+ *
756
+ * @param inputSpecFile - Openapi specification input file to calculate it's hash.
757
+ * Does not taken into account if input spec is hosted on remote resource
758
+ * @return openapi specification file hash
759
+ * @throws IOException
760
+ */
761
+ private String calculateInputSpecHash (File inputSpecFile ) throws IOException {
762
+
763
+ URL inputSpecRemoteUrl = inputSpecRemoteUrl ();
764
+
765
+ File inputSpecTempFile = inputSpecFile ;
766
+
767
+ if (inputSpecRemoteUrl != null ) {
768
+ inputSpecTempFile = File .createTempFile ("openapi-spec" , ".tmp" );
769
+
770
+ ReadableByteChannel readableByteChannel = Channels .newChannel (inputSpecRemoteUrl .openStream ());
771
+
772
+ FileOutputStream fileOutputStream = new FileOutputStream (inputSpecTempFile );
773
+ FileChannel fileChannel = fileOutputStream .getChannel ();
774
+
775
+ fileChannel .transferFrom (readableByteChannel , 0 , Long .MAX_VALUE );
776
+ }
777
+
778
+ ByteSource inputSpecByteSource =
779
+ inputSpecTempFile .exists ()
780
+ ? Files .asByteSource (inputSpecTempFile )
781
+ : CharSource .wrap (ClasspathHelper .loadFileFromClasspath (inputSpecTempFile .toString ().replaceAll ("\\ \\ " ,"/" )))
782
+ .asByteSource (Charsets .UTF_8 );
783
+
784
+ return inputSpecByteSource .hash (Hashing .sha256 ()).toString ();
785
+ }
786
+
787
+ /**
788
+ * Try to parse inputSpec setting string into URL
789
+ * @return A valid URL or null if inputSpec is not a valid URL
790
+ */
791
+ private URL inputSpecRemoteUrl (){
792
+ try {
793
+ return new URI (inputSpec ).toURL ();
794
+ } catch (URISyntaxException e ) {
795
+ return null ;
796
+ } catch (MalformedURLException e ) {
797
+ return null ;
798
+ }
799
+ }
800
+
801
+ /**
802
+ * Get specification hash file
803
+ * @param inputSpecFile - Openapi specification input file to calculate it's hash.
804
+ * Does not taken into account if input spec is hosted on remote resource
805
+ * @return a file with previously calculated hash
806
+ */
749
807
private File getHashFile (File inputSpecFile ) {
750
- return new File (output .getPath () + File .separator + ".openapi-generator" + File .separator + inputSpecFile .getName () + ".sha256" );
808
+ String name = inputSpecFile .getName ();
809
+
810
+ URL url = inputSpecRemoteUrl ();
811
+ if (url != null ) {
812
+ String [] segments = url .getPath ().split ("/" );
813
+ name = Files .getNameWithoutExtension (segments [segments .length - 1 ]);
814
+ }
815
+
816
+ return new File (output .getPath () + File .separator + ".openapi-generator" + File .separator + name + ".sha256" );
751
817
}
752
818
753
819
private String getCompileSourceRoot () {
@@ -757,8 +823,7 @@ private String getCompileSourceRoot() {
757
823
final String sourceFolder =
758
824
sourceFolderObject == null ? "src/main/java" : sourceFolderObject .toString ();
759
825
760
- String sourceJavaFolder = output .toString () + "/" + sourceFolder ;
761
- return sourceJavaFolder ;
826
+ return output .toString () + "/" + sourceFolder ;
762
827
}
763
828
764
829
private void addCompileSourceRootIfConfigured () {
@@ -803,4 +868,4 @@ private void adjustAdditionalProperties(final CodegenConfig config) {
803
868
}
804
869
}
805
870
}
806
- }
871
+ }
0 commit comments