All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.yawenok.apns.http2.impl.handler.HttpResponseHandler Maven / Gradle / Ivy

package io.github.yawenok.apns.http2.impl.handler;

import com.alibaba.fastjson.JSON;
import io.github.yawenok.apns.http2.ApnsConstant;
import io.github.yawenok.apns.http2.impl.model.ErrorResponse;
import io.github.yawenok.apns.http2.impl.model.ResponseFuture;
import io.github.yawenok.apns.http2.NotificationResponse;
import io.github.yawenok.apns.http2.impl.model.ResponseFutureCallback;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.util.CharsetUtil;
import io.netty.util.internal.PlatformDependent;

import java.util.Map;

@ChannelHandler.Sharable
public class HttpResponseHandler extends SimpleChannelInboundHandler {
    private final Map apnsRespMap = PlatformDependent.newConcurrentHashMap();

    public ResponseFuture putResponseFuture(final String apnsId, final ResponseFuture responseFuture) {
        return (ResponseFuture) apnsRespMap.put(apnsId, responseFuture);
    }

    public ResponseFutureCallback putResponseFutureCallback(final String apnsId, final ResponseFutureCallback responseFutureCallback) {
        return (ResponseFutureCallback) apnsRespMap.put(apnsId, responseFutureCallback);
    }

    public Object cleanApns(final String apnsId) {
        return apnsRespMap.remove(apnsId);
    }

    @Override
    protected void channelRead0(final ChannelHandlerContext ctx, final FullHttpResponse msg) throws Exception {
        final String apnsId = msg.headers().get(ApnsConstant.APNS_NOTIFICATION_ID);
        final Object future = apnsRespMap.remove(apnsId);
        if (future == null) {
            System.err.println("Message received for unknown apnsId " + apnsId);
        } else {
            // 填充结果
            NotificationResponse response = new NotificationResponse();
            response.setCode(msg.status().code());

            ByteBuf content = msg.content();
            if (content.isReadable()) {
                int len = content.readableBytes();
                byte[] data = new byte[len];
                content.readBytes(data);

                try {
                    ErrorResponse error = JSON.parseObject(new String(data, 0, len, CharsetUtil.UTF_8), ErrorResponse.class);
                    if (error != null) {
                        response.setReason(error.getReason());
                        response.setTimestamp(error.getTimestamp());
                    }
                } catch (Exception e) {
                    System.err.println("Json parse error! " + e.getMessage());
                }
            }

            if (future instanceof ResponseFuture) {
                ResponseFuture responseFuture = (ResponseFuture) future;
                responseFuture.setResponse(response);
                return;
            }

            if (future instanceof ResponseFutureCallback) {
                ResponseFutureCallback responseFutureCallback = (ResponseFutureCallback) future;
                responseFutureCallback.completed(response);
                return;
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy