0

I am working with an ICAP github project located here which is based off JBoss Netty codecs. My current problem which likely stems from my inability to properly understand netty handlers and Httpchunks along with lack of programming skills is I do not know how to write the data returned to a file successfully. Basically my code so far is able to push content to a file but the file is always corrupt whether it is I am missing data, not performing it correctly, or am putting it together wrong I am not entirely sure. The code is below. Any suggestions or assistance I would love. Thanks in advance.

package ch.mimo.netty.example.icap.preview;

import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;

import ch.mimo.netty.handler.codec.icap.DefaultIcapResponse;
import ch.mimo.netty.handler.codec.icap.IcapChunk;
import ch.mimo.netty.handler.codec.icap.IcapChunkTrailer;
import ch.mimo.netty.handler.codec.icap.IcapRequest;
import ch.mimo.netty.handler.codec.icap.IcapResponse;
import ch.mimo.netty.handler.codec.icap.IcapResponseStatus;
import ch.mimo.netty.handler.codec.icap.IcapVersion;

public class IcapServerHandler extends SimpleChannelUpstreamHandler {

    private boolean continueWasSent;

    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
        Object msg = e.getMessage();
        if(msg instanceof IcapRequest) {
            IcapRequest request = (IcapRequest)e.getMessage();
            System.out.println(request.toString());
        } else if(msg instanceof IcapChunkTrailer) {
            System.out.println(msg.toString());
            if(!continueWasSent) {
                continueWasSent = true;
                // sending 100 continue in order to receive the rest of the message
                IcapResponse response = new DefaultIcapResponse(IcapVersion.ICAP_1_0,IcapResponseStatus.CONTINUE);
                ctx.getChannel().write(response);
            } else {
                // sending 204 No Content response
                IcapResponse response = new DefaultIcapResponse(IcapVersion.ICAP_1_0,IcapResponseStatus.NO_CONTENT);
                ctx.getChannel().write(response);
            }
        } else if(msg instanceof IcapChunk) {
            IcapChunk request3 = (IcapChunk)e.getMessage();
           System.out.println(request3.getContent().toString(Charset.defaultCharset()));
            testfile = "C:\\Users\\dan\\Desktop\\iso\\netty\\" + output;
            buffer = request3.getContent();
            ByteBuffer nioBuffer = buffer.toByteBuffer();
            FileOutputStream fos = new FileOutputStream(testfile);
            FileChannel channel = fos.getChannel();
            while (nioBuffer.hasRemaining()) {
                channel.write(nioBuffer);
            }
            fos.flush();
            fos.close();
            System.out.println(msg);
        } 
    }

}

Also, I should say for small text files I am able to successfully rebuild the file locally. However, say with larger .docx files when I try to build the file I am not getting the full size and the file is corrupt. At this point I really am not sure what to try next.

djoemart
  • 9
  • 4
  • Your issue is caused by the fact that 1 file may exists out of multiple `IcapChunk`'s, but I'm not experienced with the Icap protocol to give you instructions to to merge those packets together, but the following handler might help you: https://github.com/jmimo/netty-icap/blob/master/src/main/java/ch/mimo/netty/handler/codec/icap/IcapChunkAggregator.java – Ferrybig Oct 23 '18 at 12:15
  • Thanks for your advise. Not sure why but once I changed the conversion from file to bytes using the below code it worked for me. file2bytestream = Files.readAllBytes(testfilePath); – djoemart Oct 24 '18 at 00:22

0 Answers0