Search in sources :

Example 6 with BaseMessage

use of com.pogeyan.cmis.api.BaseMessage in project copper-cms by PogeyanOSS.

the class GatewayActor method onReceive.

@SuppressWarnings("unlikely-arg-type")
@Override
public void onReceive(Object message) throws Exception {
    if (message instanceof BaseMessage) {
        BaseMessage bm = (BaseMessage) message;
        if (bm.getTypeName().equals("entry")) {
            String senderName = this.getTypeName(this.getSender());
            if (LOG.isDebugEnabled()) {
                LOG.debug("entry message received for sender: {}", senderName);
            }
            MemberUpRequest memberUpRequest = (MemberUpRequest) bm.getMessageAsType(MemberUpRequest.class);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Adding {} with methods {} to ref list", senderName, memberUpRequest.getSelectors());
            }
            actorRefs.put(new ActorKey(senderName, memberUpRequest.getSelectors()), this.getSender());
            getContext().watch(getSender());
        } else if (bm.getMessageType() == MessageType.REQUEST) {
            if (!this.messageRefs.containsKey(bm.getMessageId())) {
                this.messageRefs.put(bm.getMessageId(), this.getSender());
                // get actor either with type name or action name
                ActorRef actorReference = this.getActorRefBy(bm.getTypeName(), bm.getActionName());
                if (actorReference != null) {
                    // get type name from actor reference
                    String typeName = this.getTypeName(actorReference);
                    bm.setTypeName(typeName);
                    actorReference.tell(bm, this.getSelf());
                    if (Helpers.isPerfMode()) {
                        this.requestMeter.mark();
                        Timer.Context timerContext = MetricsInputs.get().getTimer("TimerID_" + bm.getMessageId()).time();
                        perfTimerContext.put(bm.getMessageId(), timerContext);
                    }
                } else {
                    LOG.error("Actor not found for {}", bm.getTypeName());
                    BaseResponse errorResponse = BaseResponse.error("Actor not available for: " + bm.getMessageId() + ", using this actor: " + bm.getTypeName(), 500);
                    BaseMessage respMessage = BaseMessage.create("ERROR", "ERROR", errorResponse);
                    respMessage.setMessageType(MessageType.ERROR);
                    this.getSender().tell(respMessage, null);
                    if (Helpers.isPerfMode()) {
                        this.errorMeter.mark();
                    }
                }
            } else {
                BaseResponse errorResponse = BaseResponse.error("Message id already present --> " + bm.getMessageId(), 404);
                LOG.error("Message id already present: {}", bm.getMessageId());
                BaseMessage respMessage = BaseMessage.create("ERROR", "ERROR", errorResponse);
                respMessage.setMessageType(MessageType.ERROR);
                this.getSender().tell(respMessage, null);
                if (Helpers.isPerfMode()) {
                    this.errorMeter.mark();
                    this.baseResponseErrorMeter.mark();
                }
            }
        } else {
            if (this.messageRefs.containsKey((bm.getMessageId()))) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Reply back to servlet actor for message: {}", bm);
                }
                ActorRef responseActorRef = this.messageRefs.get(bm.getMessageId());
                this.messageRefs.remove(bm.getMessageId());
                responseActorRef.tell(bm, ActorRef.noSender());
                if (Helpers.isPerfMode()) {
                    this.responseMeter.mark();
                    Timer.Context timerContext = this.perfTimerContext.get(bm.getMessageId());
                    if (timerContext != null) {
                        timerContext.stop();
                        this.perfTimerContext.remove(bm.getMessageId());
                    }
                }
            } else {
                LOG.error("Unknown message in messageRefs : {}", bm.getMessageId());
                unhandled(bm);
            }
        }
    } else if (message instanceof Terminated) {
        Terminated terminated = (Terminated) message;
        String[] s = terminated.getActor().path().name().split(Pattern.quote("."));
        String terminatedActor = s[s.length - 1];
        LOG.info("Actor terminated: {}", terminatedActor);
        ActorRef tActor = actorRefs.remove(terminatedActor);
        getContext().unwatch(tActor);
    } else {
        LOG.error("Unknown message received: {}", message != null ? message.toString() : "");
        unhandled(message);
    }
}
Also used : BaseResponse(com.pogeyan.cmis.api.BaseResponse) BaseMessage(com.pogeyan.cmis.api.BaseMessage) ActorRef(akka.actor.ActorRef) MemberUpRequest(com.pogeyan.cmis.api.messages.MemberUpRequest) Terminated(akka.actor.Terminated)

Aggregations

BaseMessage (com.pogeyan.cmis.api.BaseMessage)6 CmisNotSupportedException (org.apache.chemistry.opencmis.commons.exceptions.CmisNotSupportedException)5 IOException (java.io.IOException)4 CmisPermissionDeniedException (org.apache.chemistry.opencmis.commons.exceptions.CmisPermissionDeniedException)4 ActorRef (akka.actor.ActorRef)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 ServletException (javax.servlet.ServletException)3 CmisUnauthorizedException (org.apache.chemistry.opencmis.commons.exceptions.CmisUnauthorizedException)3 ActorSystem (akka.actor.ActorSystem)2 IUserObject (com.pogeyan.cmis.api.auth.IUserObject)2 QueryGetRequest (com.pogeyan.cmis.api.messages.QueryGetRequest)2 POSTHttpServletRequestWrapper (com.pogeyan.cmis.browser.shared.POSTHttpServletRequestWrapper)2 QueryStringHttpServletRequestWrapper (com.pogeyan.cmis.browser.shared.QueryStringHttpServletRequestWrapper)2 AsyncContext (javax.servlet.AsyncContext)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 ObjectData (org.apache.chemistry.opencmis.commons.data.ObjectData)2 ActorSelection (akka.actor.ActorSelection)1 Props (akka.actor.Props)1 Terminated (akka.actor.Terminated)1