List of usage examples for org.apache.commons.io.input TeeInputStream TeeInputStream
public TeeInputStream(InputStream input, OutputStream branch)
From source file:org.apache.nifi.processors.standard.InvokeHTTP.java
@Override public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException { OkHttpClient okHttpClient = okHttpClientAtomicReference.get(); FlowFile requestFlowFile = session.get(); // Checking to see if the property to put the body of the response in an attribute was set boolean putToAttribute = context.getProperty(PROP_PUT_OUTPUT_IN_ATTRIBUTE).isSet(); if (requestFlowFile == null) { if (context.hasNonLoopConnection()) { return; }// w ww . j a v a 2 s .c o m String request = context.getProperty(PROP_METHOD).evaluateAttributeExpressions().getValue() .toUpperCase(); if ("POST".equals(request) || "PUT".equals(request) || "PATCH".equals(request)) { return; } else if (putToAttribute) { requestFlowFile = session.create(); } } // Setting some initial variables final int maxAttributeSize = context.getProperty(PROP_PUT_ATTRIBUTE_MAX_LENGTH).asInteger(); final ComponentLog logger = getLogger(); // Every request/response cycle has a unique transaction id which will be stored as a flowfile attribute. final UUID txId = UUID.randomUUID(); FlowFile responseFlowFile = null; try { // read the url property from the context final String urlstr = trimToEmpty( context.getProperty(PROP_URL).evaluateAttributeExpressions(requestFlowFile).getValue()); final URL url = new URL(urlstr); Request httpRequest = configureRequest(context, session, requestFlowFile, url); // log request logRequest(logger, httpRequest); // emit send provenance event if successfully sent to the server if (httpRequest.body() != null) { session.getProvenanceReporter().send(requestFlowFile, url.toExternalForm(), true); } final long startNanos = System.nanoTime(); Response responseHttp = okHttpClient.newCall(httpRequest).execute(); // output the raw response headers (DEBUG level only) logResponse(logger, url, responseHttp); // store the status code and message int statusCode = responseHttp.code(); String statusMessage = responseHttp.message(); if (statusCode == 0) { throw new IllegalStateException("Status code unknown, connection hasn't been attempted."); } // Create a map of the status attributes that are always written to the request and response FlowFiles Map<String, String> statusAttributes = new HashMap<>(); statusAttributes.put(STATUS_CODE, String.valueOf(statusCode)); statusAttributes.put(STATUS_MESSAGE, statusMessage); statusAttributes.put(REQUEST_URL, url.toExternalForm()); statusAttributes.put(TRANSACTION_ID, txId.toString()); if (requestFlowFile != null) { requestFlowFile = session.putAllAttributes(requestFlowFile, statusAttributes); } // If the property to add the response headers to the request flowfile is true then add them if (context.getProperty(PROP_ADD_HEADERS_TO_REQUEST).asBoolean() && requestFlowFile != null) { // write the response headers as attributes // this will overwrite any existing flowfile attributes requestFlowFile = session.putAllAttributes(requestFlowFile, convertAttributesFromHeaders(url, responseHttp)); } boolean outputBodyToRequestAttribute = (!isSuccess(statusCode) || putToAttribute) && requestFlowFile != null; boolean outputBodyToResponseContent = (isSuccess(statusCode) && !putToAttribute) || context.getProperty(PROP_OUTPUT_RESPONSE_REGARDLESS).asBoolean(); ResponseBody responseBody = responseHttp.body(); boolean bodyExists = responseBody != null; InputStream responseBodyStream = null; SoftLimitBoundedByteArrayOutputStream outputStreamToRequestAttribute = null; TeeInputStream teeInputStream = null; try { responseBodyStream = bodyExists ? responseBody.byteStream() : null; if (responseBodyStream != null && outputBodyToRequestAttribute && outputBodyToResponseContent) { outputStreamToRequestAttribute = new SoftLimitBoundedByteArrayOutputStream(maxAttributeSize); teeInputStream = new TeeInputStream(responseBodyStream, outputStreamToRequestAttribute); } if (outputBodyToResponseContent) { /* * If successful and putting to response flowfile, store the response body as the flowfile payload * we include additional flowfile attributes including the response headers and the status codes. */ // clone the flowfile to capture the response if (requestFlowFile != null) { responseFlowFile = session.create(requestFlowFile); } else { responseFlowFile = session.create(); } // write attributes to response flowfile responseFlowFile = session.putAllAttributes(responseFlowFile, statusAttributes); // write the response headers as attributes // this will overwrite any existing flowfile attributes responseFlowFile = session.putAllAttributes(responseFlowFile, convertAttributesFromHeaders(url, responseHttp)); // transfer the message body to the payload // can potentially be null in edge cases if (bodyExists) { // write content type attribute to response flowfile if it is available if (responseBody.contentType() != null) { responseFlowFile = session.putAttribute(responseFlowFile, CoreAttributes.MIME_TYPE.key(), responseBody.contentType().toString()); } if (teeInputStream != null) { responseFlowFile = session.importFrom(teeInputStream, responseFlowFile); } else { responseFlowFile = session.importFrom(responseBodyStream, responseFlowFile); } // emit provenance event final long millis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos); if (requestFlowFile != null) { session.getProvenanceReporter().fetch(responseFlowFile, url.toExternalForm(), millis); } else { session.getProvenanceReporter().receive(responseFlowFile, url.toExternalForm(), millis); } } } // if not successful and request flowfile is not null, store the response body into a flowfile attribute if (outputBodyToRequestAttribute && bodyExists) { String attributeKey = context.getProperty(PROP_PUT_OUTPUT_IN_ATTRIBUTE) .evaluateAttributeExpressions(requestFlowFile).getValue(); if (attributeKey == null) { attributeKey = RESPONSE_BODY; } byte[] outputBuffer; int size; if (outputStreamToRequestAttribute != null) { outputBuffer = outputStreamToRequestAttribute.getBuffer(); size = outputStreamToRequestAttribute.size(); } else { outputBuffer = new byte[maxAttributeSize]; size = StreamUtils.fillBuffer(responseBodyStream, outputBuffer, false); } String bodyString = new String(outputBuffer, 0, size, getCharsetFromMediaType(responseBody.contentType())); requestFlowFile = session.putAttribute(requestFlowFile, attributeKey, bodyString); final long millis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos); session.getProvenanceReporter().modifyAttributes(requestFlowFile, "The " + attributeKey + " has been added. The value of which is the body of a http call to " + url.toExternalForm() + ". It took " + millis + "millis,"); } } finally { if (outputStreamToRequestAttribute != null) { outputStreamToRequestAttribute.close(); outputStreamToRequestAttribute = null; } if (teeInputStream != null) { teeInputStream.close(); teeInputStream = null; } else if (responseBodyStream != null) { responseBodyStream.close(); responseBodyStream = null; } } route(requestFlowFile, responseFlowFile, session, context, statusCode); } catch (final Exception e) { // penalize or yield if (requestFlowFile != null) { logger.error("Routing to {} due to exception: {}", new Object[] { REL_FAILURE.getName(), e }, e); requestFlowFile = session.penalize(requestFlowFile); requestFlowFile = session.putAttribute(requestFlowFile, EXCEPTION_CLASS, e.getClass().getName()); requestFlowFile = session.putAttribute(requestFlowFile, EXCEPTION_MESSAGE, e.getMessage()); // transfer original to failure session.transfer(requestFlowFile, REL_FAILURE); } else { logger.error("Yielding processor due to exception encountered as a source processor: {}", e); context.yield(); } // cleanup response flowfile, if applicable try { if (responseFlowFile != null) { session.remove(responseFlowFile); } } catch (final Exception e1) { logger.error("Could not cleanup response flowfile due to exception: {}", new Object[] { e1 }, e1); } } }
From source file:org.codice.ddf.transformer.xml.streaming.lib.SaxEventHandlerDelegate.java
public TeeInputStream getMetadataStream(InputStream inputStream, OutputStream outputStream) { return new TeeInputStream(inputStream, outputStream); }
From source file:org.collectionspace.chain.controller.WebUIRequest.java
private void initRequest(UIUmbrella umbrella, HttpServletRequest request, HttpServletResponse response, List<String> p) throws IOException, UIException { this.request = request; this.response = response; boolean isMultipart = ServletFileUpload.isMultipartContent(request); if (isMultipart) { // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(); // Parse the request FileItemIterator iter;//from w w w . ja v a2 s .c o m try { iter = upload.getItemIterator(request); while (iter.hasNext()) { FileItemStream item = iter.next(); String name = item.getFieldName(); //InputStream stream = item.openStream(); if (item.isFormField()) { // System.out.println("Form field " + name + " with value " // + Streams.asString(stream) + " detected."); } else { // System.out.println("File field " + name + " with file name " // + item.getName() + " detected."); // Process the input stream contentHeaders = item.getHeaders(); uploadName = item.getName(); ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); if (item != null) { InputStream stream = item.openStream(); IOUtils.copy(stream, byteOut); new TeeInputStream(stream, byteOut); } bytebody = byteOut.toByteArray(); } } } catch (FileUploadException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { body = IOUtils.toString(request.getInputStream(), "UTF-8"); } this.ppath = p.toArray(new String[0]); if (!(umbrella instanceof WebUIUmbrella)) throw new UIException("Bad umbrella"); this.umbrella = (WebUIUmbrella) umbrella; session = calculateSessionId(); }
From source file:org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument.java
public void setResponse(HttpMethod method, int status) throws IOException, DocumentException { this.status = status; InputStream stream = method.getResponseBodyAsStream(); SAXReader reader = new SAXReader(); if (status >= 400) { log.error("Got error : " + IOUtils.toString(stream)); }// w w w .j a v a2s .co m // TODO errorhandling Document out = null; Header content_type = method.getResponseHeader("Content-Type"); if (content_type != null && "application/xml".equals(content_type.getValue())) { if (log.isDebugEnabled()) { ByteArrayOutputStream dump = new ByteArrayOutputStream(); // TODO CSPACE-2552 add ,"UTF-8" to reader.read()? out = reader.read(new TeeInputStream(stream, dump)); log.debug(dump.toString("UTF-8")); } else { // TODO CSPACE-2552 add ,"UTF-8" to reader.read()? out = reader.read(stream); } } stream.close(); doc = out; }
From source file:org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument.java
public void setResponse(HttpMethod method, int status) throws Exception { setStatus(status);/*from w ww . j av a2 s. com*/ InputStream stream = method.getResponseBodyAsStream(); SAXReader reader = new SAXReader(); if (isErrorStatus()) { log.info("Got error : " + IOUtils.toString(stream)); } // TODO errorhandling Document doc = null; Header content_type = method.getResponseHeader("Content-Type"); if (content_type != null && "application/xml".equals(content_type.getValue())) { if (log.isDebugEnabled()) { ByteArrayOutputStream dump = new ByteArrayOutputStream(); doc = reader.read(new TeeInputStream(stream, dump)); log.debug(dump.toString("UTF-8")); } else { doc = reader.read(stream, "UTF-8"); } //split up document Element root = doc.getRootElement(); // iterate through child elements of root for (Iterator i = root.elementIterator(); i.hasNext();) { Element element = (Element) i.next(); addDocument(element.getName(), DocumentHelper.parseText(element.asXML())); } } stream.close(); }
From source file:org.collectionspace.chain.csp.persistence.services.connection.ServicesConnection.java
private void doRequest(Returned out, RequestMethod method_type, String uri, RequestDataSource src, CSPRequestCredentials creds, CSPRequestCache cache) throws ConnectionException { InputStream body_data = null; if (src != null) { body_data = src.getStream();//from w w w . ja v a 2 s . c o m } try { HttpMethod method = createMethod(method_type, uri, body_data); if (body_data != null) { method.setRequestHeader("Content-Type", src.getMIMEType()); // XXX Not sure if or when this ever actually writes to stderr? body_data = new TeeInputStream(body_data, System.err); } try { HttpClient client = makeClient(creds, cache); String requestContext = null; if (perflog.isDebugEnabled()) { // TODO add more context, e.g. session id? requestContext = "HttpClient@" + Integer.toHexString(client.hashCode()); requestContext += "/CSPRequestCache@" + Integer.toHexString(cache.hashCode()) + ","; //String queryString = method.getQueryString(); perflog.debug(System.currentTimeMillis() + ",\"" + Thread.currentThread().getName() + "\",app,svc," + requestContext + method.getName() + " " + method.getURI() //+ (queryString!=null ? queryString : "") ); } int response = client.executeMethod(method); if (perflog.isDebugEnabled()) { perflog.debug(System.currentTimeMillis() + ",\"" + Thread.currentThread().getName() + "\",svc,app," + requestContext + "HttpClient.executeMethod done"); } out.setResponse(method, response); } catch (ConnectionException e) { throw new ConnectionException(e.getMessage(), e.status, base_url + "/" + uri, e); } catch (Exception e) { throw new ConnectionException(e.getMessage(), 0, base_url + "/" + uri, e); } finally { method.releaseConnection(); if (log.isWarnEnabled()) { if (manager.getConnectionsInPool() >= MAX_SERVICES_CONNECTIONS) { log.warn("reached max services connection limit of " + MAX_SERVICES_CONNECTIONS); // Delete closed connections from the pool, so that the warning will cease // once a connection becomes available. manager.deleteClosedConnections(); } } } } finally { closeStream(body_data); } }
From source file:org.cryptomator.frontend.webdav.filters.RecordingServletInputStream.java
public RecordingServletInputStream(ServletInputStream delegate) { this.delegate = delegate; this.teeInputStream = new TeeInputStream(delegate, recording); }
From source file:org.dataconservancy.dcs.ingest.services.util.ElmSipStager.java
public Dcp getSIP(String key) { synchronized (key.intern()) { InputStream stream = null; try {//from w w w. j av a 2s . c om stream = entityStore.get(key); } catch (EntityNotFoundException e) { return null; } final ByteArrayOutputStream out = new ByteArrayOutputStream(); final TeeInputStream tee = new TeeInputStream(stream, out); try { return modelBuilder.buildSip(tee); } catch (InvalidXmlException e) { try { log.error("Invalid XML in the SIP with key [{}]: {}\n{}\n", new Object[] { key, e.getMessage(), IOUtils.toString(new ByteArrayInputStream(out.toByteArray())) }); } catch (IOException ioe) { } throw new RuntimeException(e); } catch (RuntimeException e) { try { log.error("Error reading SIP with key [{}]: {}\n{}\n", new Object[] { key, e.getMessage(), IOUtils.toString(new ByteArrayInputStream(out.toByteArray())) }); } catch (IOException ioe) { } throw new RuntimeException(e); } finally { try { out.close(); tee.close(); stream.close(); } catch (IOException e) { } } } }
From source file:org.dataconservancy.dcs.ingest.util.SeadElmSipStager.java
public ResearchObject getSIP(String key) { synchronized (key.intern()) { InputStream stream = null; try {/*from w w w .j a va 2 s . c om*/ stream = entityStore.get(key); } catch (EntityNotFoundException e) { return null; } final ByteArrayOutputStream out = new ByteArrayOutputStream(); final TeeInputStream tee = new TeeInputStream(stream, out); try { return (ResearchObject) modelBuilder.buildSip(tee); } catch (InvalidXmlException e) { try { log.error("Invalid XML in the SIP with key [{}]: {}\n{}\n", new Object[] { key, e.getMessage(), IOUtils.toString(new ByteArrayInputStream(out.toByteArray())) }); } catch (IOException ioe) { } throw new RuntimeException(e); } catch (RuntimeException e) { try { log.error("Error reading SIP with key [{}]: {}\n{}\n", new Object[] { key, e.getMessage(), IOUtils.toString(new ByteArrayInputStream(out.toByteArray())) }); } catch (IOException ioe) { } throw new RuntimeException(e); } finally { try { out.close(); tee.close(); stream.close(); } catch (IOException e) { } } } }
From source file:org.dataconservancy.ui.model.builder.xstream.XstreamBusinessObjectBuilder.java
@Override public Bop buildBusinessObjectPackage(InputStream in) throws InvalidXmlException { Assertion.notNull(in);//from www. j a va 2s . c o m final Bop bop; try { if (validating) { ByteArrayOutputStream sink = new ByteArrayOutputStream(8192); in = new TeeInputStream(in, sink); bop = (Bop) x.fromXML(in); validator.validate(new StreamSource(new ByteArrayInputStream(sink.toByteArray()))); } else { bop = (Bop) x.fromXML(in); } } catch (StreamException e) { log.debug(String.format(DESER_ERR, e.getMessage()), e); throw new InvalidXmlException(e); } catch (SAXException e) { log.debug(String.format(DESER_ERR, e.getMessage()), e); throw new InvalidXmlException(e); } catch (IOException e) { log.debug(String.format(DESER_ERR, e.getMessage()), e); throw new InvalidXmlException(e); } return bop; }