private String getRequestBody(StaplerRequest request) { String requestBody; try { Charset charset = request.getCharacterEncoding() == null ? UTF_8 : Charset.forName(request.getCharacterEncoding()); requestBody = IOUtils.toString(request.getInputStream(), charset); } catch (IOException e) { throw HttpResponses.error(500, "Failed to read request body"); } return requestBody; }
/** * Installs a list of plugins from a JSON POST. * @param req The request object. * @return A JSON response that includes a "correlationId" in the "data" element. * That "correlationId" can then be used in calls to * {@link UpdateCenter#doInstallStatus(org.kohsuke.stapler.StaplerRequest)}. * @throws IOException Error reading JSON payload fro request. */ @RequirePOST @Restricted(DoNotUse.class) // WebOnly public HttpResponse doInstallPlugins(StaplerRequest req) throws IOException { Jenkins.getInstance().checkPermission(Jenkins.ADMINISTER); String payload = IOUtils.toString(req.getInputStream(), req.getCharacterEncoding()); JSONObject request = JSONObject.fromObject(payload); JSONArray pluginListJSON = request.getJSONArray("plugins"); List<String> plugins = new ArrayList<>(); for (int i = 0; i < pluginListJSON.size(); i++) { plugins.add(pluginListJSON.getString(i)); } UUID correlationId = UUID.randomUUID(); try { boolean dynamicLoad = request.getBoolean("dynamicLoad"); install(plugins, dynamicLoad, correlationId); JSONObject responseData = new JSONObject(); responseData.put("correlationId", correlationId.toString()); return hudson.util.HttpResponses.okJSON(responseData); } catch (Exception e) { return hudson.util.HttpResponses.errorJSON(e.getMessage()); } }
public static <T> T fromRequest(StaplerRequest req, Class<T> to) throws IOException { String contentEncoding = req.getCharacterEncoding(); StringWriter writer = new StringWriter(); if (contentEncoding == null) { contentEncoding = "UTF-8"; } Util.copyStreamAndClose(new InputStreamReader(req.getInputStream(), contentEncoding), writer); return fromString(writer.toString(), to); }
private WebHookPayload parse(StaplerRequest req) throws IOException { //TODO Actually test what duckerhub is really sending String body = IOUtils.toString(req.getInputStream(), req.getCharacterEncoding()); String contentType = req.getContentType(); if (contentType != null && contentType.startsWith("application/x-www-form-urlencoded")) { body = URLDecoder.decode(body, req.getCharacterEncoding()); } logger.log(Level.FINER, "Received commit hook notification : {0}", body); try { JSONObject payload = JSONObject.fromObject(body); return createPushNotification(payload); } catch (Exception e) { logger.log(Level.SEVERE, "Could not parse the web hook payload!", e); return null; } }
@Override public Object parse(final StaplerRequest request, final StringBodyParameter a, final Class type, final String parameterName) throws ServletException { final String rawContentType = request.getContentType(); final String contentType = StringHelper.determineContentTypeWithoutCharset(rawContentType); if (MediaType.APPLICATION_FORM_URLENCODED.equals(contentType)) { return request.getParameter(parameterName); } // default to application/json final String characterEncoding = request.getCharacterEncoding(); try { return IOUtils.toString(request.getInputStream(), characterEncoding); } catch (final IOException e) { LOGGER.log(Level.SEVERE, "Unable to obtain request body: {}", e.getMessage()); } return null; }
/** * returns the value of the key as a String. if multiple values have been * submitted, the first one will be returned. * * @param key * @return */ private String getString(String key) throws javax.servlet.ServletException, java.io.IOException { if (isMultipartEncoded) { // borrowed from org.kohsuke.staple.RequestImpl FileItem item = parsedFormData.get(key); if (item!=null && item.isFormField()) { if (item.getContentType() == null && request.getCharacterEncoding() != null) { // JENKINS-11543: If client doesn't set charset per part, use request encoding try { return item.getString(request.getCharacterEncoding()); } catch (java.io.UnsupportedEncodingException uee) { LOGGER.log(Level.WARNING, "Request has unsupported charset, using default for '"+key+"' parameter", uee); return item.getString(); } } else { return item.getString(); } } else { throw new IllegalArgumentException("Parameter not found: " + key); } } else { return (String) (((Object[]) request.getParameterMap().get(key))[0]); } }
String encoding = req.getCharacterEncoding(); payload = URLDecoder.decode(body.substring(PAYLOAD_LENGTH), encoding != null ? encoding : "UTF-8"); } catch (UnsupportedEncodingException e) {
/** * Installs a list of plugins from a JSON POST. * @param req The request object. * @return A JSON response that includes a "correlationId" in the "data" element. * That "correlationId" can then be used in calls to * {@link UpdateCenter#doInstallStatus(org.kohsuke.stapler.StaplerRequest)}. * @throws IOException Error reading JSON payload fro request. */ @RequirePOST @Restricted(DoNotUse.class) // WebOnly public HttpResponse doInstallPlugins(StaplerRequest req) throws IOException { Jenkins.getInstance().checkPermission(Jenkins.ADMINISTER); String payload = IOUtils.toString(req.getInputStream(), req.getCharacterEncoding()); JSONObject request = JSONObject.fromObject(payload); JSONArray pluginListJSON = request.getJSONArray("plugins"); List<String> plugins = new ArrayList<>(); for (int i = 0; i < pluginListJSON.size(); i++) { plugins.add(pluginListJSON.getString(i)); } UUID correlationId = UUID.randomUUID(); try { boolean dynamicLoad = request.getBoolean("dynamicLoad"); install(plugins, dynamicLoad, correlationId); JSONObject responseData = new JSONObject(); responseData.put("correlationId", correlationId.toString()); return hudson.util.HttpResponses.okJSON(responseData); } catch (Exception e) { return hudson.util.HttpResponses.errorJSON(e.getMessage()); } }
public void doDynamic(StaplerRequest request, StaplerResponse response) throws Exception { String pathInfo = StringUtils.stripEnd(request.getPathInfo(), "/"); String jobName = pathInfo.substring(pathInfo.lastIndexOf("/") + 1); final String encoding = request.getCharacterEncoding(); jobName = new String(encoding == null ? jobName.getBytes() : jobName.getBytes(encoding), "UTF-8"); AbstractProject target = (AbstractProject) Hudson.getInstance().getItem(jobName);