public void write(IApimanBuffer chunk) { if (fileOS == null) { try { fileOS = new FileOutputStream(tmpFile); fileOS.write(chunk.getBytes()); } catch (IOException e) { // TODO: Log the error. // The response will not be cached as it might not contain a valid body writeFailed = true; } } }
@Override public void write(IApimanBuffer chunk) { buffer.append(chunk); } @Override
/** * Search for the given character in the buffer, starting at the * given index. If not found, return -1. If found, return the * index of the character. * @param c * @param index */ private int findFrom(char c, int index) { int currentIdx = index; while (currentIdx < buffer.length()) { if (buffer.get(currentIdx) == c) { return currentIdx; } currentIdx++; } return -1; }
private Buffer getNativeBuffer(IApimanBuffer buffer) { if (buffer.getNativeBuffer() instanceof Buffer) { return (Buffer) buffer.getNativeBuffer(); } else { logger.debug("Received an IApimanBuffer with a non-Vert.x implementation. " //$NON-NLS-1$ + "This will function but may require copying and be less efficient."); //$NON-NLS-1$ return Buffer.buffer(buffer.getBytes()); } } }
/** * @see io.apiman.gateway.engine.io.IWriteStream#write(io.apiman.gateway.engine.io.IApimanBuffer) */ @Override public void write(IApimanBuffer chunk) { try { if (!connected) { throw new IOException("Not connected."); //$NON-NLS-1$ } if (outputStream == null) { outputStream = connection.getOutputStream(); } if (chunk instanceof ByteBuffer) { byte[] buffer = (byte[]) chunk.getNativeBuffer(); outputStream.write(buffer, 0, chunk.length()); } else { outputStream.write(chunk.getBytes()); } } catch (IOException e) { handleConnectionError(e); } }
/** * @see io.apiman.gateway.engine.io.IApimanBuffer#append(io.apiman.gateway.engine.io.IApimanBuffer) */ @Override public void append(IApimanBuffer buffer) { append(buffer, 0, buffer.length()); }
if (buffer == null || buffer.length() == 0) { return; int maxPos = buffer.length() - 1; boolean done = false; IApimanBuffer originalBuffer = buffer; int urlsFound = 0; while (!done) { if (originalBuffer.get(position) == (byte) 'h' || originalBuffer.get(position) == (byte) 'H') { if (isURLStart()) { urlsFound++; IApimanBuffer preUrlData = bufferFactory.createBuffer(originalBuffer.getBytes(preUrlFromPos, position)); super.write(preUrlData); bytesConsumed = position; urlBuffer = bufferFactory.createBuffer(url, "UTF-8"); //$NON-NLS-1$ } else { urlBuffer = bufferFactory.createBuffer(originalBuffer.getBytes(originalPos, position)); buffer = null; } else { buffer = bufferFactory.createBuffer(originalBuffer.getBytes(bytesConsumed, maxPos + 1)); if (atEnd) { super.write(buffer);
/** * @return the URL consumed from the buffer */ private String consumeURL() { String rval = null; int pos = position + "http://".length(); //$NON-NLS-1$ while (pos < buffer.length() && isValidURLChar(pos)) { pos++; } if (pos < buffer.length() || atEnd) { try { rval = buffer.getString(position, pos, "UTF-8"); //$NON-NLS-1$ } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } position = pos; } return rval; }
/** * Append the given data to any existing buffer, then scan the buffer * looking for the soap headers. If scanning is complete, this method * will return true. If more data is required, then the method will return * false. If an error condition is detected, then an exception will be * thrown. * @param buffer */ public boolean scan(IApimanBuffer buffer) throws SoapEnvelopeNotFoundException { if (this.buffer == null) { this.buffer = buffer; } else { this.buffer.append(buffer); } boolean scanComplete = doScan(); // If our buffer is already "max size" but we haven't found the start of the // soap envelope yet, then we're likely not going to find it. if (!scanComplete && this.buffer.length() >= getMaxBufferLength()) { throw new SoapEnvelopeNotFoundException(); } return scanComplete; }
public VertxApimanBuffer(IApimanBuffer buffer) { this.nativeBuffer = Buffer.buffer(buffer.length()).appendBuffer((Buffer) buffer.getNativeBuffer()); }
@Override public void write(IApimanBuffer buffer) { write((byte[]) buffer.getNativeBuffer()); }
/** * @return the xml preamble found during scanning */ public String getXmlPreamble() { try { return buffer.getString(xmlPreamble.startIdx, xmlPreamble.endIdx + 1, "UTF-8"); //$NON-NLS-1$ } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } }
/** * @param pos the position to check * @return true if the character at position 'pos' is a valid URL character */ private boolean isValidURLChar(int pos) { byte b = buffer.get(pos); char ch = (char) b; // Valid URL characters as defined here: http://tools.ietf.org/html/rfc3986#section-2 return Character.isDigit(ch) || Character.isLetter(ch) || ch == '-' || ch == '.' || ch == '_' || ch == '~' || ch == ':' || ch == '/' || ch == '?' || ch == '#' || ch == '[' || ch == ']' || ch == '@' || ch == '!' || ch == '$' || ch == '&' || ch == '\'' || ch == '(' || ch == ')' || ch == '*' || ch == '+' || ch == ',' || ch == ';' || ch == '='; }
/** * @see io.apiman.gateway.engine.io.IWriteStream#write(io.apiman.gateway.engine.io.IApimanBuffer) */ @Override public void write(IApimanBuffer chunk) { try { if (!connected) { throw new IOException("Not connected."); //$NON-NLS-1$ } if (outputStream == null) { outputStream = connection.getOutputStream(); } if (chunk instanceof ByteBuffer) { byte[] buffer = (byte[]) chunk.getNativeBuffer(); outputStream.write(buffer, 0, chunk.length()); } else { outputStream.write(chunk.getBytes()); } } catch (IOException e) { handleConnectionError(e); } }
@Override public void write(IApimanBuffer chunk) { total += chunk.length(); super.write(chunk); } @Override
/** * @param index */ private int consumeEnvelopeDecl(int index) { int end = findFrom('>', index); if (end == -1) { // Not enough buffer - need more data. return buffer.length(); } try { String str = buffer.getString(index, end + 1, "UTF-8"); //$NON-NLS-1$ String [] split = str.split(" "); //$NON-NLS-1$ if (split[0].endsWith("Envelope")) { //$NON-NLS-1$ envelopeDecl.startIdx = index; envelopeDecl.endIdx = end; return end + 1; } } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } // We found a section of XML that should have been a soap:Envelope, but wasn't. return -1; }
/** * Append the given data to any existing buffer, then scan the buffer * looking for the soap headers. If scanning is complete, this method * will return true. If more data is required, then the method will return * false. If an error condition is detected, then an exception will be * thrown. * @param buffer */ public boolean scan(IApimanBuffer buffer) throws SoapEnvelopeNotFoundException { if (this.buffer == null) { this.buffer = buffer; } else { this.buffer.append(buffer); } boolean scanComplete = doScan(); // If our buffer is already "max size" but we haven't found the start of the // soap envelope yet, then we're likely not going to find it. if (!scanComplete && this.buffer.length() >= getMaxBufferLength()) { throw new SoapEnvelopeNotFoundException(); } return scanComplete; }
@Override public void insert(int index, IApimanBuffer buffer) { nativeBuffer.setBuffer(index, (Buffer) buffer.getNativeBuffer()); }