private List<Element> getStories(@Nullable final String query, final int max) throws Exception { String url = API_URL + "/projects/" + myProjectId + "/stories"; url += "?filter=" + encodeUrl("state:started,unstarted,unscheduled,rejected"); if (!StringUtil.isEmpty(query)) { url += encodeUrl(" \"" + query + '"'); } if (max >= 0) { url += "&limit=" + encodeUrl(String.valueOf(max)); } LOG.info("Getting all the stories with url: " + url); final HttpMethod method = doREST(url, HTTPMethod.GET); final InputStream stream = method.getResponseBodyAsStream(); final Element element = new SAXBuilder(false).build(stream).getRootElement(); if (!"stories".equals(element.getName())) { LOG.warn("Error fetching issues for: " + url + ", HTTP status code: " + method.getStatusCode()); throw new Exception("Error fetching issues for: " + url + ", HTTP status code: " + method.getStatusCode() + "\n" + element.getText()); } return element.getChildren("story"); }
public void selectVerb(HTTPMethod verb) { switch (verb) { case GET: buttonGETRequest.setSelected(true); requestPayloadSection.setVisible(false); requestPayloadSection.clearPayload(); break; case POST: buttonPOSTRequest.setSelected(true); requestPayloadSection.setVisible(true); break; case DELETE: buttonDELETERequest.setSelected(true); requestPayloadSection.setVisible(true); break; case PUT: buttonPUTRequest.setSelected(true); requestPayloadSection.setVisible(true); break; default: throw new IllegalArgumentException("unsupported method " + verb); } httpMethod = verb; }
@Override public void setTaskState(Task task, TaskState state) throws Exception { if (state != TaskState.IN_PROGRESS) super.setTaskState(task, state); final String realId = getRealId(task.getId()); if (realId == null) return; String url = API_URL + "/projects/" + myProjectId + "/stories/" + realId; url +="?" + encodeUrl("story[current_state]") + "=" + encodeUrl("started"); LOG.info("Updating issue state by id: " + url); final HttpMethod method = doREST(url, HTTPMethod.PUT); final InputStream stream = method.getResponseBodyAsStream(); final Element element = new SAXBuilder(false).build(stream).getRootElement(); final Task story = element.getName().equals("story") ? createIssue(element) : null; if (story == null) { throw new Exception("Error setting state for: " + url + ", HTTP status code: " + method.getStatusCode() + "\n" + element.getText()); } }
@Test public void testHttpOperations() { //see swagger spec, all but 'trace' operations are supported Collection<String> allButTrace = Arrays.stream(HTTPMethod.values()) .filter(m -> m != HTTPMethod.TRACE) .map(HTTPMethod::name) .map(String::toLowerCase) .sorted() .collect(Collectors.toList()); getCaretCompletions("http_operations") .assertContains(allButTrace) .assertContains("$ref") .assertNotContains("trace"); }
public void resetToDefaults() { myLoginURL = ""; myTasksListUrl = ""; mySingleTaskUrl = ""; myDownloadTasksInSeparateRequests = false; myLoginMethodType = HTTPMethod.GET; myTasksListMethodType = HTTPMethod.GET; mySingleTaskMethodType = HTTPMethod.GET; myResponseType = ResponseType.XML; myTemplateVariables = new ArrayList<TemplateVariable>(); myResponseHandlersMap = new EnumMap<ResponseType, ResponseHandler>(ResponseType.class); myResponseHandlersMap.put(ResponseType.XML, getXmlResponseHandlerDefault()); myResponseHandlersMap.put(ResponseType.JSON, getJsonResponseHandlerDefault()); myResponseHandlersMap.put(ResponseType.TEXT, getTextResponseHandlerDefault()); }
private HttpMethod doREST(final String request, final HTTPMethod type) throws Exception { final HttpClient client = getHttpClient(); client.getParams().setContentCharset("UTF-8"); final String uri = getUrl() + request; final HttpMethod method = type == HTTPMethod.POST ? new PostMethod(uri) : type == HTTPMethod.PUT ? new PutMethod(uri) : new GetMethod(uri); configureHttpMethod(method); client.executeMethod(method); return method; }
@Nullable @Override public Task findTask(@NotNull final String id) throws Exception { final String realId = getRealId(id); if (realId == null) return null; final String url = API_URL + "/projects/" + myProjectId + "/stories/" + realId; LOG.info("Retrieving issue by id: " + url); final HttpMethod method = doREST(url, HTTPMethod.GET); final InputStream stream = method.getResponseBodyAsStream(); final Element element = new SAXBuilder(false).build(stream).getRootElement(); return element.getName().equals("story") ? createIssue(element) : null; }
@Override public void setTaskState(@NotNull Task task, @NotNull TaskState state) throws Exception { final String realId = getRealId(task.getId()); if (realId == null) return; final String stateName; switch (state) { case IN_PROGRESS: stateName = "started"; break; case RESOLVED: stateName = "finished"; break; // may add some others in future default: return; } String url = API_URL + "/projects/" + myProjectId + "/stories/" + realId; url += "?" + encodeUrl("story[current_state]") + "=" + encodeUrl(stateName); LOG.info("Updating issue state by id: " + url); final HttpMethod method = doREST(url, HTTPMethod.PUT); final InputStream stream = method.getResponseBodyAsStream(); final Element element = new SAXBuilder(false).build(stream).getRootElement(); if (!element.getName().equals("story")) { if (element.getName().equals("errors")) { throw new Exception(extractErrorMessage(element)); } else { // unknown error, probably our fault LOG.warn("Error setting state for: " + url + ", HTTP status code: " + method.getStatusCode()); throw new Exception(String.format("Cannot set state '%s' for issue.", stateName)); } } }
public void createMethodFromExample(RestExampleModel methodModel) { // method name String methodName = methodModel.getName(); methodNameSection.getMethodNamePanel().setText(methodName); // set verb String urlWithVerb = methodModel.getRequestUrl(); String[] parts = urlWithVerb.split(" "); HTTPMethod verb = HTTPMethod.GET; if (parts.length > 1) { verb = HTTPMethod.valueOf(parts[0]); } type.selectVerb(verb); // set url details (paths/queries String templatizedUrl = parts[parts.length - 1]; methodNameSection.populateUrlDetails(templatizedUrl); // Request Headers Map<String, String> headers = methodModel.getRequestHeaders(); if (headers != null && !headers.isEmpty()) { header.setEnabled(true); for (Map.Entry<String, String> e : headers.entrySet()) { header.addHeader(e.getKey(), e.getValue()); } } // Request body setSectionBody(requestPayloadSection, methodModel.getRequestBody()); // Response body setSectionBody(responsePayloadSection, methodModel.getResponseBody()); }
@Nullable @Override public Task findTask(final String id) throws Exception { final String realId = getRealId(id); if (realId == null) return null; final String url = API_URL + "/projects/" + myProjectId + "/stories/" + realId; LOG.info("Retrieving issue by id: " + url); final HttpMethod method = doREST(url, HTTPMethod.GET); final InputStream stream = method.getResponseBodyAsStream(); final Element element = new SAXBuilder(false).build(stream).getRootElement(); return element.getName().equals("story") ? createIssue(element) : null; }
@NotNull public static RequestBuilder head(@NotNull String url) { RequestBuilder builder = request(url); builder.myMethod = HTTPMethod.HEAD; return builder; }
private HttpMethod getHttpMethod(String requestUrl, HTTPMethod type) { HttpMethod method = type == HTTPMethod.GET ? new GetMethod(requestUrl) : GenericRepositoryUtil.getPostMethodFromURL(requestUrl); configureHttpMethod(method); return method; }
public void setLoginMethodType(final HTTPMethod loginMethodType) { myLoginMethodType = loginMethodType; }
public void setTasksListMethodType(final HTTPMethod tasksListMethodType) { myTasksListMethodType = tasksListMethodType; }
public void setSingleTaskMethodType(HTTPMethod singleTaskMethodType) { mySingleTaskMethodType = singleTaskMethodType; }
public HTTPMethod getLoginMethodType() { return myLoginMethodType; }
public HTTPMethod getTasksListMethodType() { return myTasksListMethodType; }
public HTTPMethod getSingleTaskMethodType() { return mySingleTaskMethodType; }
public HTTPMethod getHttpMethod() { return httpMethod; }
public void setHttpMethod(HTTPMethod httpMethod) { this.httpMethod = httpMethod; }
public void actionPerformed(ActionEvent e) { requestPayloadSection.setVisible(true); httpMethod = HTTPMethod.POST; }
public void actionPerformed(ActionEvent e) { requestPayloadSection.setVisible(true); httpMethod = HTTPMethod.PUT; }
public void actionPerformed(ActionEvent e) { requestPayloadSection.setVisible(false); requestPayloadSection.clearPayload(); httpMethod = HTTPMethod.GET; }
public void actionPerformed(ActionEvent e) { requestPayloadSection.setVisible(false); requestPayloadSection.clearPayload(); httpMethod = HTTPMethod.DELETE; }