@Override public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception { Object message = writeRequest.getMessage(); ProtocolEncoder encoder = getEncoder(session); ProtocolEncoderOutput encoderOut = new DirectOutput(session, nextFilter, writeRequest); try { encoder.encode(session, message, encoderOut); nextFilter.filterWrite(session, new EndOfMessage(writeRequest)); } catch (ProtocolEncoderException e) { throw e; } catch (Throwable t) { throw new ProtocolEncoderException(t); } }
/** * Handle the exception we got. * * @param session The session we got the exception on * @param cause The exception cause * @throws Exception If we have had another exception */ @Override public void exceptionCaught( IoSession session, Throwable cause ) throws Exception { if ( LOG.isWarnEnabled() ) { LOG.warn( cause.getMessage(), cause ); } session.setAttribute( EXCEPTION_KEY, cause ); if ( cause instanceof ProtocolEncoderException ) { Throwable realCause = ( ( ProtocolEncoderException ) cause ).getCause(); if ( realCause instanceof MessageEncoderException ) { int messageId = ( ( MessageEncoderException ) realCause ).getMessageId(); ResponseFuture<?> response = futureMap.get( messageId ); response.cancel( true ); response.setCause( realCause ); } } session.closeNow(); }
/** * Handle the exception we got. * * @param session The session we got the exception on * @param cause The exception cause * @throws Exception If we have had another exception */ @Override public void exceptionCaught( IoSession session, Throwable cause ) throws Exception { if ( LOG.isWarnEnabled() ) { LOG.warn( cause.getMessage(), cause ); } session.setAttribute( EXCEPTION_KEY, cause ); if ( cause instanceof ProtocolEncoderException ) { Throwable realCause = ( ( ProtocolEncoderException ) cause ).getCause(); if ( realCause instanceof MessageEncoderException ) { int messageId = ( ( MessageEncoderException ) realCause ).getMessageId(); ResponseFuture<?> response = futureMap.get( messageId ); response.cancel( true ); response.setCause( realCause ); } } session.closeNow(); }
private String calculateContentType(String contentType) throws ProtocolEncoderException { if (contentType == null) { return CONTENT_TYPE_TEXT_PLAIN_CHARSET_UTF_8; } // text/???[;charset=???] => text/plain[;charset=???] if (contentType.startsWith(CONTENT_TYPE_PREFIX_TEXT)) { int charsetAt = contentType.indexOf(';'); if (charsetAt != -1) { String charsetName = contentType.substring(charsetAt + 1).trim(); if (!ASCII_COMPATIBLE.contains(charsetName.toLowerCase())) { throw new ProtocolEncoderException("HTTP enveloping not compatible with charset: " + charsetName); } return format("%s;%s", CONTENT_TYPE_TEXT_PLAIN, charsetName); } else { return CONTENT_TYPE_TEXT_PLAIN; } } // non-text => application/octet-stream return CONTENT_TYPE_APPLICATION_OCTET_STREAM; }
/** * Handle the exception we got. * * @param session The session we got the exception on * @param cause The exception cause * @throws Exception If we have had another exception */ @Override public void exceptionCaught( IoSession session, Throwable cause ) throws Exception { if ( LOG.isWarnEnabled() ) { LOG.warn( cause.getMessage(), cause ); } session.setAttribute( EXCEPTION_KEY, cause ); if ( cause instanceof ProtocolEncoderException ) { Throwable realCause = ( ( ProtocolEncoderException ) cause ).getCause(); if ( realCause instanceof MessageEncoderException ) { int messageId = ( ( MessageEncoderException ) realCause ).getMessageId(); ResponseFuture<?> response = futureMap.get( messageId ); response.cancel( true ); response.setCause( realCause ); } } session.closeNow(); }
public void encode( IoSession session, Object message, ProtocolEncoderOutput out ) throws Exception { Class type = message.getClass(); MessageEncoder encoder = findEncoder( type ); if( encoder == null ) { throw new ProtocolEncoderException( "Unexpected message type: " + type ); } encoder.encode( session, message, out ); }
@Override public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception { if (message == null) { return; } if (String.class.isInstance(message)) { encodeString(0, session, (String) message, out); } Class<?> clz = message.getClass(); for (SumkMinaEncoder encoder : this.encoders) { if (encoder.accept(clz)) { encoder.encode(session, message, out); return; } } throw new ProtocolEncoderException(message.getClass().getName() + " not support in ProtocolEncoder"); }
private static void putProtocol(int code, IoBuffer buffer, int prefixLength) throws ProtocolEncoderException { switch (prefixLength) { case 1: buffer.putInt(Protocols.ONE | code | Protocols.MAGIC); break; case 2: buffer.putInt(Protocols.TWO | code | Protocols.MAGIC); break; case 4: buffer.putInt(Protocols.FOUR | code | Protocols.MAGIC); break; default: throw new ProtocolEncoderException("error size"); } }
@Override protected void encode(IoSessionEx session, HttpMessage httpMessage, ProtocolEncoderOutput out) throws Exception { switch (httpMessage.getKind()) { case CONTENT: HttpContentMessage httpContent = (HttpContentMessage)httpMessage; encodeContent(session, httpContent, out); break; case REQUEST: HttpRequestMessage httpRequest = (HttpRequestMessage)httpMessage; encodeRequest(session, httpRequest, out); break; default: throw new ProtocolEncoderException("Unexpected HTTP message kind: " + httpMessage.getKind()); } }
@Override protected void encode(IoSessionEx session, HttpMessage httpMessage, ProtocolEncoderOutput out) throws Exception { switch (httpMessage.getKind()) { case CONTENT: HttpContentMessage httpContent = (HttpContentMessage)httpMessage; encodeContent(session, httpContent, out); break; case RESPONSE: HttpResponseMessage httpResponse = (HttpResponseMessage)httpMessage; encodeResponse(session, httpResponse, out); break; default: throw new ProtocolEncoderException("Unexpected HTTP message kind: " + httpMessage.getKind()); } }
String charset = innerContentType.substring(charsetAt + 1); if (!ASCII_COMPATIBLE.contains(charset.toLowerCase())) { throw new ProtocolEncoderException("HTTP enveloping not compatible with charset: " + charset);
throw new ProtocolEncoderException("The encoder is null for the session " + session); pee = (ProtocolEncoderException) e; } else { pee = new ProtocolEncoderException(e);
throw new ProtocolEncoderException("The encoder is null for the session " + session); pee = (ProtocolEncoderException) e; } else { pee = new ProtocolEncoderException(e);
@Override public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception { Object message = writeRequest.getMessage(); // Bypass the encoding if the message is contained in a IoBuffer, // as it has already been encoded before if (message instanceof IoBuffer || message instanceof FileRegion) { nextFilter.filterWrite(session, writeRequest); return; } // Get the encoder in the session ProtocolEncoder encoder = getEncoder(session); ProtocolEncoderOutput encoderOut = getEncoderOut(session, nextFilter, writeRequest); try { // Now we can try to encode the response encoder.encode(session, message, encoderOut); // Flush the encoded message (assumes single encoded message) ((ProtocolEncoderOutputImpl) encoderOut).flushWithFuture(nextFilter, session, writeRequest); } catch (Throwable t) { ProtocolEncoderException pee; // Generate the correct exception if (t instanceof ProtocolEncoderException) { pee = (ProtocolEncoderException) t; } else { pee = new ProtocolEncoderException(t); } throw pee; } }
pee = (ProtocolEncoderException) t; } else { pee = new ProtocolEncoderException(t);