77 */
88package com .intellij .lang .jsgraphql .ide .introspection ;
99
10- import com .fasterxml .jackson .databind .ObjectMapper ;
1110import com .google .gson .Gson ;
1211import com .google .gson .JsonSyntaxException ;
1312import com .intellij .ide .actions .CreateFileAction ;
6261import com .intellij .util .concurrency .annotations .RequiresWriteLock ;
6362import com .intellij .util .messages .MessageBusConnection ;
6463import org .apache .commons .lang .StringEscapeUtils ;
65- import org .apache .commons .lang .StringUtils ;
6664import org .apache .http .client .methods .CloseableHttpResponse ;
6765import org .apache .http .client .methods .HttpPost ;
6866import org .apache .http .client .methods .HttpUriRequest ;
8078import org .jetbrains .annotations .Nullable ;
8179
8280import java .io .IOException ;
83-
84- import java .security .GeneralSecurityException ;
85- import java .security .KeyManagementException ;
86- import java .security .KeyStoreException ;
87- import java .security .NoSuchAlgorithmException ;
88- import java .util .Collection ;
89- import java .util .Collections ;
90- import java .util .List ;
91- import java .util .Map ;
9281import java .nio .file .Path ;
9382import java .nio .file .Paths ;
9483import java .security .*;
9584import java .security .cert .CertificateException ;
85+ import java .util .Collection ;
86+ import java .util .Collections ;
87+ import java .util .List ;
88+ import java .util .Map ;
9689
9790import static com .intellij .lang .jsgraphql .ide .project .GraphQLUIProjectService .setHeadersFromOptions ;
9891
@@ -102,8 +95,6 @@ public class GraphQLIntrospectionService implements Disposable {
10295 private static final String DISABLE_EMPTY_ERRORS_WARNING_KEY = "graphql.empty.errors.warning.disabled" ;
10396 public static final String GRAPHQL_TRUST_ALL_HOSTS = "graphql.trust.all.hosts" ;
10497
105- public static final String SSL_EXTENSION = "sslConfiguration" ;
106-
10798 private GraphQLIntrospectionTask latestIntrospection = null ;
10899 private final Project myProject ;
109100
@@ -192,30 +183,37 @@ public static HttpPost createRequest(@NotNull GraphQLConfigVariableAwareEndpoint
192183 return request ;
193184 }
194185
195- public GraphQLConfigSecurity getSecurityConfig (@ NotNull VirtualFile file ) {
186+ @ Nullable
187+ public GraphQLConfigSecurity getSecurityConfig (@ Nullable VirtualFile configFile ) {
188+ if (configFile == null ) {
189+ return null ;
190+ }
191+
192+ GraphQLConfigData config = GraphQLConfigManager .getService (myProject ).getConfigurationsByPath ()
193+ .get (configFile .isDirectory () ? configFile : configFile .getParent ());
194+ if (config == null ) {
195+ return null ;
196+ }
196197
197- GraphQLConfigData config = GraphQLConfigManager .getService (myProject ).getConfigurationsByPath ().get (file );
198- Map <String , Object > sslExtension = (Map <String , Object >) config .extensions .get (SSL_EXTENSION );
199- if (sslExtension != null && ! sslExtension .isEmpty ()) {
198+ Map <String , Object > sslExtension = (Map <String , Object >) config .extensions .get (GraphQLConfigManager .SSL_EXTENSION );
199+ if (sslExtension != null && !sslExtension .isEmpty ()) {
200200 GraphQLConfigSecurity sslConfig = new GraphQLConfigSecurity ();
201201 Map <String , Object > clientCertificate = (Map <String , Object >) sslExtension .get ("clientCertificate" );
202- if (clientCertificate != null && ! clientCertificate .isEmpty ()) {
202+ if (clientCertificate != null && !clientCertificate .isEmpty ()) {
203203 sslConfig .clientCertificate = new GraphQLConfigCertificate ();
204- String path = (String ) clientCertificate .get ("path" );
205- sslConfig .clientCertificate .path = path ;
204+ sslConfig .clientCertificate .path = (String ) clientCertificate .get ("path" );
206205 String format = (String ) clientCertificate .get ("format" );
207- if (format != null && ! format .equals ("PEM" )) {
206+ if (format != null && !format .equals ("PEM" )) {
208207 throw new RuntimeException ("Unsupported certificate format, only PEM is currently supported" );
209208 }
210209 sslConfig .clientCertificate .format = GraphQLConfigCertificate .Encoding .PEM ;
211210 }
212211 Map <String , Object > clientCertificateKey = (Map <String , Object >) sslExtension .get ("clientCertificateKey" );
213- if (clientCertificateKey != null && ! clientCertificateKey .isEmpty ()) {
212+ if (clientCertificateKey != null && !clientCertificateKey .isEmpty ()) {
214213 sslConfig .clientCertificateKey = new GraphQLConfigCertificate ();
215- String path = (String ) clientCertificateKey .get ("path" );
216- sslConfig .clientCertificateKey .path = path ;
214+ sslConfig .clientCertificateKey .path = (String ) clientCertificateKey .get ("path" );
217215 String format = (String ) clientCertificateKey .get ("format" );
218- if (format != null && ! format .equals ("PEM" )) {
216+ if (format != null && !format .equals ("PEM" )) {
219217 throw new RuntimeException ("Unsupported certificate format, only PEM is currently supported" );
220218 }
221219 sslConfig .clientCertificateKey .format = GraphQLConfigCertificate .Encoding .PEM ;
@@ -536,7 +534,7 @@ public IntrospectionQueryTask(@NotNull HttpUriRequest request,
536534 public void run (@ NotNull ProgressIndicator indicator ) {
537535 indicator .setIndeterminate (true );
538536 String responseJson ;
539- GraphQLConfigSecurity sslConfig = getSecurityConfig (introspectionSourceFile . getParent () );
537+ GraphQLConfigSecurity sslConfig = getSecurityConfig (introspectionSourceFile );
540538 try (final CloseableHttpClient httpClient = createHttpClient (sslConfig );
541539 final CloseableHttpResponse response = httpClient .execute (request )) {
542540 responseJson = ObjectUtils .coalesce (EntityUtils .toString (response .getEntity ()), "" );
0 commit comments