@Nullable private static String getFileTemplateContent(@NotNull String filename) { try { // replace on windows, just for secure reasons return StreamUtil.readText(PhpBundleFileFactory.class.getResourceAsStream(filename), "UTF-8") .replace("\r\n", "\n"); } catch (IOException e) { return null; } }
public Task[] getIssues(@Nullable String request, int max, long since) throws Exception { String query = getDefaultSearch(); if (request != null) { query += " " + request; } String requestUrl = "/rest/project/issues/?filter=" + encodeUrl(query) + "&max=" + max + "&updatedAfter" + since; HttpMethod method = doREST(requestUrl, false); InputStream stream = method.getResponseBodyAsStream(); // todo workaround for http://youtrack.jetbrains.net/issue/JT-7984 String s = StreamUtil.readText(stream, "UTF-8"); for (int i = 0; i < s.length(); i++) { if (!XMLChar.isValid(s.charAt(i))) { s = s.replace(s.charAt(i), ' '); } } Element element; try { // InputSource source = new InputSource(stream); // source.setEncoding("UTF-8"); // element = new SAXBuilder(false).build(source).getRootElement(); element = new SAXBuilder(false).build(new StringReader(s)).getRootElement(); } catch (JDOMException e) { LOG.error("Can't parse YouTrack response for " + requestUrl, e); throw e; } if ("error".equals(element.getName())) { throw new Exception( "Error from YouTrack for " + requestUrl + ": '" + element.getText() + "'"); } List<Element> children = element.getChildren("issue"); final List<Task> tasks = ContainerUtil.mapNotNull( children, new NullableFunction<Element, Task>() { public Task fun(Element o) { return createIssue(o); } }); return tasks.toArray(new Task[tasks.size()]); }
@NotNull public String executeMethod(@NotNull HttpMethod method) throws Exception { LOG.debug("URI: " + method.getURI()); int statusCode; String entityContent; try { statusCode = getHttpClient().executeMethod(method); LOG.debug("Status code: " + statusCode); // may be null if 204 No Content received final InputStream stream = method.getResponseBodyAsStream(); entityContent = stream == null ? "" : StreamUtil.readText(stream, CharsetToolkit.UTF8); LOG.debug(entityContent); } finally { method.releaseConnection(); } // besides SC_OK, can also be SC_NO_CONTENT in issue transition requests // see: JiraRestApi#setTaskStatus // if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_NO_CONTENT) { if (statusCode >= 200 && statusCode < 300) { return entityContent; } else if (method.getResponseHeader("Content-Type") != null) { Header header = method.getResponseHeader("Content-Type"); if (header.getValue().startsWith("application/json")) { JsonObject object = GSON.fromJson(entityContent, JsonObject.class); if (object.has("errorMessages")) { String reason = StringUtil.join(object.getAsJsonArray("errorMessages"), " "); // something meaningful to user, e.g. invalid field name in JQL query LOG.warn(reason); throw new Exception("Request failed. Reason: " + reason); } } } if (method.getResponseHeader("X-Authentication-Denied-Reason") != null) { Header header = method.getResponseHeader("X-Authentication-Denied-Reason"); // only in JIRA >= 5.x.x if (header.getValue().startsWith("CAPTCHA_CHALLENGE")) { throw new Exception("Login failed. Enter captcha in web-interface."); } } if (statusCode == HttpStatus.SC_UNAUTHORIZED) { throw new Exception(LOGIN_FAILED_CHECK_YOUR_PERMISSIONS); } String statusText = HttpStatus.getStatusText(method.getStatusCode()); throw new Exception( String.format("Request failed with HTTP error: %d %s", statusCode, statusText)); }
private static int readInt(Scanner s, Process process) throws ExecutionException { long started = System.currentTimeMillis(); while (System.currentTimeMillis() - started < PORTS_WAITING_TIMEOUT) { if (s.hasNextLine()) { String line = s.nextLine(); try { return Integer.parseInt(line); } catch (NumberFormatException ignored) { continue; } } try { Thread.sleep(200); } catch (InterruptedException ignored) { } if (process.exitValue() != 0) { String error; try { error = "Console process terminated with error:\n" + StreamUtil.readText(process.getErrorStream()); } catch (Exception ignored) { error = "Console process terminated with exit code " + process.exitValue(); } throw new ExecutionException(error); } else { break; } } throw new ExecutionException("Couldn't read integer value from stream"); }
@Nullable private String createHtmlWithCodeHighlighting( @NotNull final String content, @NotNull StudyToolWindowConfigurator configurator) { String template = null; InputStream stream = getClass().getResourceAsStream("/code-mirror/template.html"); try { template = StreamUtil.readText(stream, "utf-8"); } catch (IOException e) { LOG.warn(e.getMessage()); } finally { try { stream.close(); } catch (IOException e) { LOG.warn(e.getMessage()); } } if (template == null) { LOG.warn("Code mirror template is null"); return null; } final EditorColorsScheme editorColorsScheme = EditorColorsManager.getInstance().getGlobalScheme(); int fontSize = editorColorsScheme.getEditorFontSize(); template = template.replace("${font_size}", String.valueOf(fontSize - 2)); template = template.replace( "${highlight_mode}", getClass().getResource("/code-mirror/clike.js").toExternalForm()); template = template.replace( "${codemirror}", getClass().getResource("/code-mirror/codemirror.js").toExternalForm()); template = template.replace( "${python}", getClass().getResource("/code-mirror/python.js").toExternalForm()); template = template.replace( "${runmode}", getClass().getResource("/code-mirror/runmode.js").toExternalForm()); template = template.replace( "${colorize}", getClass().getResource("/code-mirror/colorize.js").toExternalForm()); template = template.replace( "${javascript}", getClass().getResource("/code-mirror/javascript.js").toExternalForm()); if (LafManager.getInstance().getCurrentLookAndFeel() instanceof DarculaLookAndFeelInfo) { template = template.replace( "${css_oldcodemirror}", getClass().getResource("/code-mirror/codemirror-old-darcula.css").toExternalForm()); template = template.replace( "${css_codemirror}", getClass().getResource("/code-mirror/codemirror-darcula.css").toExternalForm()); } else { template = template.replace( "${css_oldcodemirror}", getClass().getResource("/code-mirror/codemirror-old.css").toExternalForm()); template = template.replace( "${css_codemirror}", getClass().getResource("/code-mirror/codemirror.css").toExternalForm()); } template = template.replace("${default-mode}", configurator.getDefaultHighlightingMode()); template = template.replace("${code}", content); return template; }