Skip to content

Commit

Permalink
Fix alternate namespace usages for update ref
Browse files Browse the repository at this point in the history
  • Loading branch information
Layoric committed Oct 19, 2023
1 parent 0248ad6 commit cf88a01
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/main/java/net/servicestack/idea/UpdateServiceStackUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.intellij.psi.PsiFile;
import net.servicestack.idea.common.Analytics;
import net.servicestack.idea.common.INativeTypesHandler;
import net.servicestack.idea.common.NativeTypesLanguage;
import org.apache.http.client.utils.URIBuilder;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -107,7 +108,9 @@ public static void updateServiceStackReference(PsiFile psiFile) {
StringBuilder javaCodeResponse = getJavaCodeResponse(serverUrl);

String javaCode = javaCodeResponse.toString();
if (!javaCode.startsWith(nativeTypesHandler.getOptionsCommentStart())) {
// We need to check PHP differently since it is the only language that requires a namespace
// declared at the top of the file.
if (!javaCode.startsWith(nativeTypesHandler.getOptionsCommentStart()) && !isValidPhpResponse(javaCode)) {
//noinspection UnresolvedPluginConfigReference
Notification notification = new Notification("ServiceStackIDEA", "Error updating reference", "Invalid response from provided BaseUrl - " + baseUrl, NotificationType.ERROR);
Notifications.Bus.notify(notification);
Expand All @@ -131,6 +134,24 @@ public static void updateServiceStackReference(PsiFile psiFile) {
}
}


private static boolean isValidPhpResponse(String phpCodeResponse) {
// First split into lines, we only need to test against the first 5
String[] phpCodeLines = phpCodeResponse.split("\n", 5);
// check the size of the array
if (phpCodeLines.length < 2) {
return false;
}

// Check if the first line is the namespace
if (!phpCodeLines[0].startsWith("<?php namespace")) {
return false;
}

// Check if the second line is the options comment start
return phpCodeLines[1].startsWith("/* Options:");
}

@NotNull
private static StringBuilder getJavaCodeResponse(StringBuilder serverUrl) throws IOException {
URL javaCodeUrl = new URL(serverUrl.toString());
Expand Down

0 comments on commit cf88a01

Please sign in to comment.