List of usage examples for org.apache.http.entity BasicHttpEntity setContent
public void setContent(InputStream inputStream)
From source file:org.jenkinsci.plugins.skytap.ConnectToVPNTunnelStep.java
private String attachVPNToConfiguration(String confId, String networkId, String vpnId) { // build url//from w w w . ja va2s.co m String requestUrl = this.buildRequestURL(confId, networkId); // create request HttpPost hp = SkytapUtils.buildHttpPostRequest(requestUrl, this.authCredentials); // add content to request - vpn identifier BasicHttpEntity he = new BasicHttpEntity(); he.setContentEncoding("gzip"); he.setContentType("application/json"); // json string for vpn id String jsonString = "{\"id\":\"" + vpnId + "\"}"; InputStream stream; try { stream = new ByteArrayInputStream(jsonString.getBytes("UTF-8")); Integer len = jsonString.getBytes("UTF-8").length; long llen = len.longValue(); he.setContent(stream); he.setContentLength(llen); } catch (UnsupportedEncodingException e) { JenkinsLogger.error("Error encoding json string for vpn id: " + e.getMessage()); } hp.setEntity(he); JenkinsLogger.log("HTTP POST request: " + hp.toString()); // execute request String httpRespBody = ""; try { httpRespBody = SkytapUtils.executeHttpRequest(hp); } catch (SkytapException e) { JenkinsLogger.error("Skytap Exception: " + e.getMessage()); } // return response return httpRespBody; }
From source file:ste.web.http.api.ApiHandler.java
/** * Note that we expect response to have a body entity set (@see HttpEntiry) * /*from w w w. jav a 2 s. co m*/ * @param request * @param response * @param context * * @throws HttpException * @throws IOException */ @Override public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { RRequest rr = null; File actionScript = null, applicationScript = null; try { rr = new RRequest(reduce(request.getRequestLine())); if (log.isLoggable(Level.FINE)) { log.fine(String.format("serving %s", rr.getPath())); } applicationScript = new File(apiroot, getApplicationScript(rr)); actionScript = new File(apiroot, getActionScript(rr)); if (log.isLoggable(Level.FINE)) { log.fine(String.format("application script path: %s", applicationScript.getAbsolutePath())); log.fine(String.format("action script path: %s", actionScript.getAbsolutePath())); } Interpreter bsh = new Interpreter(); BeanShellUtils.setup(bsh, request, response, (HttpSessionContext) context); bsh.set(VAR_SOURCE, actionScript.getAbsolutePath()); bsh.set(VAR_RREQUEST, rr); if (applicationScript.exists()) { bsh.eval(BeanShellUtils.getScript(applicationScript)); } bsh.eval(BeanShellUtils.getScript(actionScript)); Object body = bsh.get(rr.getHandler()); AbstractHttpEntity e = (AbstractHttpEntity) response.getEntity(); if (e.getContentType() == null) { e.setContentType("application/json"); } if (body != null) { if (body instanceof File) { File f = (File) body; e = new FileEntity(f); response.setEntity(e); String mimeType = MimeUtils.getInstance().getMimeType(f); e.setContentType( MimeUtils.MIME_UNKNOWN.equals(mimeType) ? "application/octet-stream" : mimeType); } else { String bodyString = String.valueOf(body); byte[] buf = bodyString.getBytes(); ByteArrayInputStream is = new ByteArrayInputStream(buf); BasicHttpEntity basicEntity = (BasicHttpEntity) e; basicEntity.setContent(is); basicEntity.setContentLength(buf.length); if (e.getContentType() == null) { e.setContentType("application/json"); } } } BeanShellUtils.cleanup(bsh, request); BeanShellUtils.setVariablesAttributes(bsh, context); } catch (FileNotFoundException e) { response.setStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_NOT_FOUND, "Script " + actionScript + " not found."); } catch (EvalError x) { String msg = x.getMessage(); if (log.isLoggable(Level.SEVERE)) { log.severe(String.format("error evaluating: %s: %s", actionScript, msg)); log.throwing(getClass().getName(), "handleError", x); } // // We shall not expose to the client any details of a server error // throw new HttpException("server erorr processing the resource - see server log for details", x); } catch (URISyntaxException x) { response.setStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_BAD_REQUEST, StringEscapeUtils.escapeHtml4(x.getMessage())); } catch (Exception x) { response.setStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_INTERNAL_SERVER_ERROR, StringEscapeUtils.escapeHtml4(x.getMessage())); } }