-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #261 from ibi-group/feature/otp-1392-download-plan…
…-query Download plan query from resource URI
- Loading branch information
Showing
9 changed files
with
65 additions
and
284 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 49 additions & 26 deletions
75
src/main/java/org/opentripplanner/middleware/utils/GraphQLUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,75 @@ | ||
package org.opentripplanner.middleware.utils; | ||
|
||
import org.eclipse.jetty.http.HttpMethod; | ||
import org.opentripplanner.middleware.bugsnag.BugsnagReporter; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.net.URI; | ||
|
||
import static org.opentripplanner.middleware.utils.ConfigUtils.getConfigPropertyAsText; | ||
|
||
public class GraphQLUtils { | ||
|
||
private GraphQLUtils() { | ||
throw new IllegalStateException("Utility class."); | ||
} | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(GraphQLUtils.class); | ||
|
||
// Lazily-initialized in getPlanQueryTemplate() | ||
private static String planQueryTemplate = null; | ||
|
||
/** | ||
* Location of the GraphQL plan query template file, as Java resource. | ||
* Location of the GraphQL default plan query template file, as URI resource. | ||
*/ | ||
public static final String PLAN_QUERY_RESOURCE = "queries/planQuery.graphql"; | ||
private static final String DEFAULT_PLAN_QUERY_RESOURCE_URI = | ||
"https://raw.githubusercontent.com/opentripplanner/otp-ui/refs/heads/master/packages/core-utils/src/planQuery.graphql"; | ||
|
||
/** | ||
* Return the full GraphQL plan file planQueryTemplate in Java string format, with {@code "} as {@code \"} | ||
* Location of the GraphQL plan query template file, as URI resource. | ||
*/ | ||
private static final String PLAN_QUERY_RESOURCE_URI = | ||
getConfigPropertyAsText( | ||
"PLAN_QUERY_RESOURCE_URI", | ||
DEFAULT_PLAN_QUERY_RESOURCE_URI | ||
); | ||
|
||
|
||
/** | ||
* Return the full GraphQL plan file planQueryTemplate. | ||
*/ | ||
public static String getPlanQueryTemplate() { | ||
if (GraphQLUtils.planQueryTemplate == null) { | ||
GraphQLUtils.planQueryTemplate = planQueryTemplateAsString(PLAN_QUERY_RESOURCE); | ||
} | ||
GraphQLUtils.planQueryTemplate = planQueryTemplateAsString(); | ||
} | ||
return GraphQLUtils.planQueryTemplate; | ||
} | ||
|
||
/** | ||
* Return a GraphQL planQueryTemplate in Java string format, with {@code "} as {@code \"} | ||
* @param resource the plan file or any GraphQL file | ||
* Return a GraphQL planQueryTemplate in Java string format, with {@code "} as {@code \"}. | ||
*/ | ||
static String planQueryTemplateAsString(String resource) { | ||
StringBuilder builder = new StringBuilder(); | ||
try (var reader = new BufferedReader(new InputStreamReader( | ||
GraphQLUtils.class.getClassLoader().getResourceAsStream(resource) | ||
))) { | ||
int value; | ||
// All this low-level stuff is just to put a \ in front of " in the string. | ||
while ((value = reader.read()) != -1) { | ||
char c = (char)value; | ||
if (c == '\n') builder.append("\\n"); | ||
else if (c == '"') builder.append("\\\""); | ||
else builder.append(c); | ||
} | ||
} catch (Exception e) { | ||
LOG.error("Can't find \"{}\" resource.", resource, e); | ||
static String planQueryTemplateAsString() { | ||
String rawPlanQuery = getPlanQueryFromResource(); | ||
if (rawPlanQuery == null) { | ||
String message = String.format("Unable to retrieve plan query from resource: %s.", GraphQLUtils.PLAN_QUERY_RESOURCE_URI); | ||
LOG.error(message); | ||
throw new IllegalStateException(message); | ||
} | ||
return builder.toString(); | ||
return rawPlanQuery.replace("\"", "\\\""); | ||
} | ||
|
||
/** | ||
* Download the plan query from resource URI. | ||
*/ | ||
static String getPlanQueryFromResource() { | ||
HttpResponseValues httpResponseValues = HttpUtils.httpRequestRawResponse( | ||
URI.create(GraphQLUtils.PLAN_QUERY_RESOURCE_URI), | ||
10, | ||
HttpMethod.GET, | ||
null, | ||
null | ||
); | ||
return httpResponseValues != null ? httpResponseValues.responseBody : null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.