1+ import com .google .gson .Gson ;
12import com .intellij .codeInsight .intention .impl .QuickEditAction ;
3+ import com .intellij .notification .Notification ;
4+ import com .intellij .notification .NotificationType ;
5+ import com .intellij .notification .Notifications ;
26import com .intellij .openapi .application .ApplicationManager ;
37import com .intellij .openapi .command .CommandProcessor ;
48import com .intellij .openapi .command .UndoConfirmationPolicy ;
@@ -78,6 +82,75 @@ private boolean containsOptionsHeader(PsiJavaFile psiJavaFile) {
7882 return false ;
7983 }
8084
85+ private boolean validateEndPoint (String url ) {
86+
87+ URL metadataUrl = null ;
88+ URIBuilder builder = null ;
89+ try {
90+ builder = new URIBuilder (url .replace ("types/java" ,"types/metadata" ));
91+ builder .addParameter ("format" ,"json" );
92+ metadataUrl = new URL (builder .build ().toString ());
93+ } catch (URISyntaxException e ) {
94+ //Log error to IDEA warning bubble/window.
95+ e .printStackTrace ();
96+ Notification notification = new Notification ("ServiceStackIDEA" , "Error Updating Reference" , "Invalid BaseUrl provided" , NotificationType .ERROR );
97+ Notifications .Bus .notify (notification );
98+ return false ;
99+ } catch (MalformedURLException e ) {
100+ e .printStackTrace ();
101+ Notification notification = new Notification ("ServiceStackIDEA" , "Error Updating Reference" , "Invalid BaseUrl provided" , NotificationType .ERROR );
102+ Notifications .Bus .notify (notification );
103+ return false ;
104+ }
105+
106+ URLConnection metadataConnection = null ;
107+ String errorMessage = null ;
108+ try {
109+ metadataConnection = metadataUrl .openConnection ();
110+ } catch (IOException e ) {
111+ errorMessage = "Problem connecting to BaseUrl - " + e .getMessage ();
112+ }
113+ metadataConnection .setRequestProperty ("content-type" , "application/json; charset=utf-8" );
114+ BufferedReader metadataBufferReader = null ;
115+ try {
116+ metadataBufferReader = new BufferedReader (
117+ new InputStreamReader (
118+ metadataConnection .getInputStream ()));
119+ } catch (IOException e ) {
120+ errorMessage = "Problem connecting to BaseUrl - " + e .getMessage ();
121+ }
122+ String metadataInputLine ;
123+ StringBuilder metadataResponse = new StringBuilder ();
124+ try {
125+ while ((metadataInputLine = metadataBufferReader .readLine ()) != null )
126+ metadataResponse .append (metadataInputLine );
127+
128+ metadataBufferReader .close ();
129+ } catch (IOException e ) {
130+ errorMessage = "Invalid response, check the BaseUrl is a valid ServiceStack endpoint - " + e .getMessage ();
131+ }
132+
133+
134+ String metadataJson = metadataResponse .toString ();
135+ Gson gson = new Gson ();
136+ try {
137+ ServiceStackMetadata metadata = gson .fromJson (metadataJson , ServiceStackMetadata .class );
138+ if (metadata == null || metadata .getConfig () == null || metadata .getConfig ().getBaseUrl () == null ) {
139+ errorMessage = "The address url is not a valid ServiceStack endpoint." ;
140+ }
141+ } catch (Exception e ) {
142+ errorMessage = "The address url is not a valid ServiceStack endpoint." ;
143+ }
144+
145+ if (errorMessage != null ) {
146+ Notification notification = new Notification ("ServiceStackIDEA" , "Error Updating Reference" , errorMessage , NotificationType .ERROR );
147+ Notifications .Bus .notify (notification );
148+ return false ;
149+ }
150+
151+ return true ;
152+ }
153+
81154 @ Override
82155 public void invoke (@ NotNull Project project , Editor editor , final PsiFile psiFile ) throws IncorrectOperationException {
83156 String code = psiFile .getText ();
@@ -100,7 +173,8 @@ public void invoke(@NotNull Project project, Editor editor, final PsiFile psiFil
100173 }
101174 }
102175 if (baseUrl == null ) {
103- //throw error
176+ Notification notification = new Notification ("ServiceStackIDEA" , "Error Updating Reference" , "BaseUrl property not found." , NotificationType .ERROR );
177+ Notifications .Bus .notify (notification );
104178 return ;
105179 }
106180 if (!baseUrl .endsWith ("/" )) {
@@ -112,9 +186,30 @@ public void invoke(@NotNull Project project, Editor editor, final PsiFile psiFil
112186 builder = new URIBuilder (baseUrl );
113187 } catch (URISyntaxException e ) {
114188 //Log error to IDEA warning bubble/window.
189+ Notification notification = new Notification ("ServiceStackIDEA" , "Error Updating Reference" , "Invalid BaseUrl provided" , NotificationType .ERROR );
190+ Notifications .Bus .notify (notification );
191+ return ;
192+ }
193+
194+ String existingPath = builder .getPath ();
195+ if (existingPath == null || existingPath .equals ("/" )) {
196+ builder .setPath ("/types/java" );
197+ } else {
198+ builder .setPath (existingPath + "/types/java" );
199+ }
200+
201+
202+ try {
203+ if (!validateEndPoint (builder .build ().toString ())) {
204+ return ;
205+ }
206+ } catch (URISyntaxException e ) {
207+ e .printStackTrace ();
208+ Notification notification = new Notification ("ServiceStackIDEA" , "Error Updating Reference" , "Unable to parse BaseUrl" , NotificationType .ERROR );
209+ Notifications .Bus .notify (notification );
115210 return ;
116211 }
117- builder . setPath ( "/types/java" );
212+
118213 for (int i = startParamsIndex ; i < linesOfCode .size (); i ++) {
119214 String configLine = linesOfCode .get (i );
120215 if (!configLine .startsWith ("//" ) && configLine .contains (":" )) {
@@ -123,7 +218,6 @@ public void invoke(@NotNull Project project, Editor editor, final PsiFile psiFil
123218 }
124219 }
125220
126-
127221 try {
128222 String serverUrl = builder .build ().toString ();
129223 URL javaCodeUrl = new URL (serverUrl );
@@ -147,9 +241,12 @@ public void invoke(@NotNull Project project, Editor editor, final PsiFile psiFil
147241 document .setText (javaCodeResponse );
148242 } else {
149243 //Show error
244+ Notification notification = new Notification ("ServiceStackIDEA" , "Error Updating Reference" , "DTO file not found." , NotificationType .ERROR );
245+ Notifications .Bus .notify (notification );
150246 }
151247 } catch (Exception e ) {
152- //Log with IDEA bubble
248+ Notification notification = new Notification ("ServiceStackIDEA" , "Error Updating Reference" , "Invalid BaseUrl provided" , NotificationType .ERROR );
249+ Notifications .Bus .notify (notification );
153250 e .printStackTrace ();
154251 }
155252 }
0 commit comments