Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.openehealth.ipf.platform.camel.ihe.hl7v3.iti55;
 
 
 
 import static org.apache.cxf.ws.addressing.JAXWSAConstants.ADDRESSING_PROPERTIES_INBOUND;
 import static org.apache.cxf.ws.addressing.JAXWSAConstants.ADDRESSING_PROPERTIES_OUTBOUND;
 import static org.openehealth.ipf.commons.ihe.hl7v3.Hl7v3NakFactory.response;
 import static org.openehealth.ipf.platform.camel.ihe.hl7v3.iti55.deferredresponse.Iti55DeferredResponseComponent.THREAD_POOL_NAME;

Service implementation for the Responding Gateway actor of the IHE ITI-55 transaction (XCPD) with support for the Deferred Response option.

Author(s):
Dmytro Rud
 
 public class Iti55Service extends AbstractHl7v3WebService implements Iti55PortType {
     private static final transient Logger LOG = LoggerFactory.getLogger(Iti55Service.class);
 
     private final ProducerTemplate producerTemplate;
     private final ExecutorService executorService;
     private final CamelContext camelContext;


    
Constructor.

Parameters:
endpoint Camel endpoint instance this Web Service corresponds to.
 
         super(endpoint.getComponent().getWsTransactionConfiguration());
         this. = endpoint;
         this. = endpoint.getCamelContext();
         this. = this..createProducerTemplate();
     }
 
         ExecutorServiceManager manager = this..getExecutorServiceManager();
 
         //Try to get one from the registry
         ExecutorService result = manager.newThreadPool(this);
         
         //Create a default one with non-daemon threads
         if (result == null){
             result = manager.newDefaultThreadPool(this);
         }
         return result;
     }
 
 
     @Override
     public String discoverPatients(String requestString) {
         return doProcess(requestString);
     }
 
     @Override
     public String discoverPatientsDeferred(String requestString) {
        return doProcess(requestString);
    }
    @Override
    protected String doProcess(String request) {
        final String requestString = request;
        final GPathResult requestXml = Hl7v3Utils.slurp(requestString);
        final String processingMode = Iti55Utils.processingMode(requestXml);
        // process regular requests in a synchronous route
        if ("I".equals(processingMode)) {
            String response = doProcess0(requestStringrequestXml);
            return response;
        }
        else if ("D".equals(processingMode)) {
            // check whether deferred response URI is specified
            final String deferredResponseUri = Iti55Utils.normalizedDeferredResponseUri(requestXml);
            if (deferredResponseUri == null) {
                Hl7v3Exception hl7v3Exception = new Hl7v3Exception();
                hl7v3Exception.setMessage("Deferred response URI is missing or not HTTP(S)");
                hl7v3Exception.setTypeCode("AE");
                hl7v3Exception.setAcknowledgementDetailCode("SYN105");
                hl7v3Exception.setQueryResponseCode("AE");
                return createNak(requestXmlhl7v3Exception);
            }
            // determine original request message ID
            final WrappedMessageContext messageContext = (WrappedMessageContextnew WebServiceContextImpl().getMessageContext();
            AddressingProperties apropos = (AddressingPropertiesmessageContext.get();
            final String requestMessageId = ((apropos != null) && (apropos.getMessageID() != null)) ?
                    apropos.getMessageID().getValue() : null;
            if (requestMessageId == null) {
                .warn("Cannot determine WS-Addressing ID of the request message");
            }
            final WsAuditDataset auditDataset = (WsAuditDatasetmessageContext.getWrappedMessage()
                    .getContextualProperty(.);
            // in a separate thread: run the route, send its result synchronously
            // to the deferred response URI, ignore all errors and ACKs
            Runnable processRouteAndNotifyTask = new Runnable() {
                
                @Override
                public void run() {
                    // Message context is a thread local object, so we need to propagate in into
                    // this new thread.  Note that the producer (see producerTemplate below) will
                    // get its own message context, precisely spoken a freshly created one.
                    WebServiceContextImpl.setMessageContext(messageContext);
                    // run the route
                    Object result = doProcess0(requestStringrequestXml);
                    // prepare and send deferred response.
                    // NB: Camel message headers will be used in Iti55DeferredResponseProducer
                    Exchange exchange = new DefaultExchange();
                    exchange.getIn().setBody(result);
                    exchange.getIn().setHeader("iti55.deferred.requestMessageId"requestMessageId);
                    exchange.getIn().setHeader("iti55.deferred.auditDataset"auditDataset);
                    AbstractWsEndpoint responseEndpoint = (AbstractWsEndpoint.getEndpoint(deferredResponseUri);
                    responseEndpoint.setAudit(.isAudit());
                    exchange = .send(responseEndpointexchange);
                    Exception exception = Exchanges.extractException(exchange);
                    if (exception != null) {
                        .error("Sending deferred response failed"exception);
                    }
               }
            };
            .submit(processRouteAndNotifyTask);
            // return an immediate MCCI ACK
            return response(requestXmlnull"MCCI_IN000002UV01"nullfalse);
        }
        else {
            Hl7v3Exception hl7v3Exception = new Hl7v3Exception();
            hl7v3Exception.setMessage(String.format("Unsupported processing mode '%s'"processingMode));
            hl7v3Exception.setTypeCode("AE");
            hl7v3Exception.setAcknowledgementDetailCode("NS250");
            hl7v3Exception.setQueryResponseCode("AE");
            return createNak(requestXmlhl7v3Exception);
        }
    }
    private String doProcess0(String requestStringGPathResult requestXml) {
        Exchange result = process(requestString);
        Exception exception = Exchanges.extractException(result);
        return (exception != null) ?
            nak(exceptionrequestXml) :
            Exchanges.resultMessage(result).getBody(String.class);
    }


    
Generates an XCPD-specific NAK from the given exception.

Parameters:
exception occurred exception.
requestXml original request as GPath object.
Returns:
NAK as XML string.
    private String nak(Exception exceptionGPathResult requestXml) {
        Hl7v3Exception hl7v3Exception;
        if (exception instanceof Hl7v3Exception) {
            hl7v3Exception = (Hl7v3Exceptionexception;
        } else {
            hl7v3Exception = new Hl7v3Exception();
            hl7v3Exception.setCause(exception);
            hl7v3Exception.setMessage(exception.getMessage());
            hl7v3Exception.setDetectedIssueManagementCode("InternalError");
            hl7v3Exception.setDetectedIssueManagementCodeSystem("1.3.6.1.4.1.19376.1.2.27.3");
        }
        return createNak(requestXmlhl7v3Exception);
    }


    
Configures outbound WS-Addressing header "Action".

Parameters:
action WS-Addressing action.
    private static void configureWsaAction(String action) {
        Message outMessage = messageContext.getWrappedMessage().getExchange().getOutMessage();
        // when WS-Addressing headers were missing from the beginning
        // TODO: is this check still necessary under CXF 2.5?
        if (outMessage == null) {
            return;
        }
        if (apropos == null) {
            apropos = new AddressingProperties();
            outMessage.put(apropos);
        }
        AttributedURIType actionHolder = new AttributedURIType();
        actionHolder.setValue(action);
        apropos.setAction(actionHolder);
    }
New to GrepCode? Check out our FAQ X