2121import static org .openapitools .codegen .config .CodegenConfiguratorUtils .*;
2222
2323import 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 ;
2433import java .util .HashMap ;
2534import java .util .List ;
2635import java .util .Map ;
@@ -429,7 +438,7 @@ public void execute() throws MojoExecutionException {
429438 if (inputSpecFile .exists ()) {
430439 File storedInputSpecHashFile = getHashFile (inputSpecFile );
431440 if (storedInputSpecHashFile .exists ()) {
432- String inputSpecHash = Files . asByteSource (inputSpecFile ). hash ( Hashing . sha256 ()). toString ( );
441+ String inputSpecHash = calculateInputSpecHash (inputSpecFile );
433442 String storedInputSpecHash = Files .asCharSource (storedInputSpecHashFile , Charsets .UTF_8 ).read ();
434443 if (inputSpecHash .equals (storedInputSpecHash )) {
435444 getLog ().info (
@@ -720,12 +729,7 @@ public void execute() throws MojoExecutionException {
720729
721730 // Store a checksum of the input spec
722731 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 );
729733
730734 if (storedInputSpecHashFile .getParent () != null && !new File (storedInputSpecHashFile .getParent ()).exists ()) {
731735 File parent = new File (storedInputSpecHashFile .getParent ());
@@ -746,8 +750,70 @@ public void execute() throws MojoExecutionException {
746750 }
747751 }
748752
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+ */
749807 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" );
751817 }
752818
753819 private String getCompileSourceRoot () {
@@ -757,8 +823,7 @@ private String getCompileSourceRoot() {
757823 final String sourceFolder =
758824 sourceFolderObject == null ? "src/main/java" : sourceFolderObject .toString ();
759825
760- String sourceJavaFolder = output .toString () + "/" + sourceFolder ;
761- return sourceJavaFolder ;
826+ return output .toString () + "/" + sourceFolder ;
762827 }
763828
764829 private void addCompileSourceRootIfConfigured () {
@@ -803,4 +868,4 @@ private void adjustAdditionalProperties(final CodegenConfig config) {
803868 }
804869 }
805870 }
806- }
871+ }
0 commit comments