Example usage for org.springframework.web.util NestedServletException NestedServletException

List of usage examples for org.springframework.web.util NestedServletException NestedServletException

Introduction

In this page you can find the example usage for org.springframework.web.util NestedServletException NestedServletException.

Prototype

public NestedServletException(String msg) 

Source Link

Document

Construct a NestedServletException with the specified detail message.

Usage

From source file:com.sybase365.mobiliser.custom.project.channels.HttpChannelEnd.java

@SuppressWarnings("unchecked")
@Override//from   w ww . jav  a2 s . c o m
public void processRequest(final HttpServletRequest request, final HttpServletResponse response)
        throws ServletException, IOException {

    LOG.debug("Incoming {} request", request.getMethod());

    checkAndPrepare(request, response, false);

    final MultiValueMap<String, String> result = (MultiValueMap<String, String>) this.converter.read(null,
            new ServletServerHttpRequest(request));

    final List<String> textList = result.get("text");
    final List<String> fromList = result.get("from");
    final List<String> toList = result.get("to");
    final List<String> typeList = result.get("type");

    if (textList == null || textList.isEmpty()) {
        throw new MissingServletRequestParameterException("text", "string");
    }

    if (fromList == null || fromList.isEmpty()) {
        throw new MissingServletRequestParameterException("from", "string");
    }

    if (toList == null || toList.isEmpty()) {
        throw new MissingServletRequestParameterException("to", "string");
    }

    final String type;
    if (null == typeList || typeList.isEmpty()) {
        type = "sms";
    } else {
        type = typeList.get(0);
    }

    final Message req = this.messagingEngine.parseSimpleTextMessage(type, textList.get(0));
    req.setSender(fromList.get(0));
    req.setRecipient(toList.get(0));

    if (LOG.isDebugEnabled()) {
        LOG.debug("{} message received for {} from {}",
                new Object[] { type, req.getRecipient(), req.getSender() });
    }

    final Future<Message> responseMessage = this.receiveCallback.receiveAndRespondMessage(req, this.channelId,
            this.incomingChannelId);

    if (LOG.isDebugEnabled()) {
        LOG.debug("Handed off message to {} for {} awaiting response", this.receiveCallback,
                this.incomingChannelId);
    }

    final Message message;
    try {
        message = responseMessage.get();

        if (message == null) {
            LOG.warn("Timed out waiting for response from {}", responseMessage);

            throw new NestedServletException("Timed out waiting for message");
        }
    } catch (final InterruptedException e) {
        Thread.currentThread().interrupt(); // reset flag

        throw new NestedServletException("Interrupted during processing", e);

    } catch (final ExecutionException e) {
        if (e.getCause() instanceof InterruptedException) {
            throw new NestedServletException( // NOSONAR
                    "Interrupted during processing", e.getCause());
        }

        throw new NestedServletException("Processing message failed", // NOSONAR
                e.getCause());
    }

    LOG.debug("Writing response back to client");

    final LinkedMultiValueMap<String, Object> responseMap = new LinkedMultiValueMap<String, Object>();

    responseMap.add("from", message.getSender().getAddress());
    responseMap.add("to", message.getRecipient().getAddress());

    if (message instanceof SmsMessage) {
        responseMap.add("text", new String(((SmsMessage) message).getText().getContent(),
                ((SmsMessage) message).getText().getCharset()));
    } else if (message instanceof UssdTextMessage) {
        responseMap.add("text", new String(((UssdTextMessage) message).getText().getContent(),
                ((UssdTextMessage) message).getText().getCharset()));
    }

    this.converter.write(responseMap, this.mediaType, new ServletServerHttpResponse(response));

}