2020import static org .apache .commons .lang3 .StringUtils .isNotEmpty ;
2121import static org .openapitools .codegen .config .CodegenConfiguratorUtils .*;
2222
23+ import io .swagger .v3 .parser .core .models .AuthorizationValue ;
2324import 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 ;
2435import java .util .HashMap ;
2536import java .util .List ;
2637import java .util .Map ;
4253import org .openapitools .codegen .CodegenConfig ;
4354import org .openapitools .codegen .CodegenConstants ;
4455import org .openapitools .codegen .DefaultGenerator ;
56+ import org .openapitools .codegen .auth .AuthParser ;
4557import org .openapitools .codegen .config .CodegenConfigurator ;
4658import org .openapitools .codegen .config .GlobalSettings ;
4759import org .sonatype .plexus .build .incremental .BuildContext ;
@@ -429,7 +441,7 @@ public void execute() throws MojoExecutionException {
429441 if (inputSpecFile .exists ()) {
430442 File storedInputSpecHashFile = getHashFile (inputSpecFile );
431443 if (storedInputSpecHashFile .exists ()) {
432- String inputSpecHash = Files . asByteSource (inputSpecFile ). hash ( Hashing . sha256 ()). toString ( );
444+ String inputSpecHash = calculateInputSpecHash (inputSpecFile );
433445 String storedInputSpecHash = Files .asCharSource (storedInputSpecHashFile , Charsets .UTF_8 ).read ();
434446 if (inputSpecHash .equals (storedInputSpecHash )) {
435447 getLog ().info (
@@ -720,12 +732,7 @@ public void execute() throws MojoExecutionException {
720732
721733 // Store a checksum of the input spec
722734 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 );
729736
730737 if (storedInputSpecHashFile .getParent () != null && !new File (storedInputSpecHashFile .getParent ()).exists ()) {
731738 File parent = new File (storedInputSpecHashFile .getParent ());
@@ -746,8 +753,75 @@ public void execute() throws MojoExecutionException {
746753 }
747754 }
748755
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+ */
749815 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" );
751825 }
752826
753827 private String getCompileSourceRoot () {
@@ -757,8 +831,7 @@ private String getCompileSourceRoot() {
757831 final String sourceFolder =
758832 sourceFolderObject == null ? "src/main/java" : sourceFolderObject .toString ();
759833
760- String sourceJavaFolder = output .toString () + "/" + sourceFolder ;
761- return sourceJavaFolder ;
834+ return output .toString () + "/" + sourceFolder ;
762835 }
763836
764837 private void addCompileSourceRootIfConfigured () {
@@ -803,4 +876,4 @@ private void adjustAdditionalProperties(final CodegenConfig config) {
803876 }
804877 }
805878 }
806- }
879+ }
0 commit comments