1 /*
2  * JBoss, Home of Professional Open Source.
3  * Copyright 2014 Red Hat, Inc., and individual contributors
4  * as indicated by the @author tags.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */

18
19 package io.undertow.servlet.core;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24
25 import javax.servlet.ServletRequest;
26 import javax.servlet.ServletResponse;
27
28 import io.undertow.io.BlockingReceiverImpl;
29 import io.undertow.io.BlockingSenderImpl;
30 import io.undertow.io.Receiver;
31 import io.undertow.io.Sender;
32 import io.undertow.server.BlockingHttpExchange;
33 import io.undertow.server.HttpServerExchange;
34 import io.undertow.servlet.handlers.ServletRequestContext;
35 import io.undertow.servlet.spec.HttpServletRequestImpl;
36 import io.undertow.servlet.spec.HttpServletResponseImpl;
37
38 /**
39  * @author Stuart Douglas
40  */

41 public class ServletBlockingHttpExchange implements BlockingHttpExchange {
42
43     private final HttpServerExchange exchange;
44
45     public ServletBlockingHttpExchange(final HttpServerExchange exchange) {
46         this.exchange = exchange;
47     }
48
49     @Override
50     public InputStream getInputStream() {
51         ServletRequest request = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY).getServletRequest();
52         try {
53             return request.getInputStream();
54         } catch (IOException e) {
55             throw new RuntimeException(e);
56         }
57     }
58
59     @Override
60     public OutputStream getOutputStream() {
61         ServletResponse response = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY).getServletResponse();
62         try {
63             return response.getOutputStream();
64         } catch (IOException e) {
65             throw new RuntimeException(e);
66         }
67     }
68
69     @Override
70     public Sender getSender() {
71         try {
72             return new BlockingSenderImpl(exchange, getOutputStream());
73         } catch (IllegalStateException e) {
74             ServletResponse response = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY).getServletResponse();
75             try {
76                 return new BlockingWriterSenderImpl(exchange, response.getWriter(), response.getCharacterEncoding());
77             } catch (IOException e1) {
78                 throw new RuntimeException(e1);
79             }
80         }
81     }
82
83     @Override
84     public void close() throws IOException {
85         ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
86         if (!exchange.isComplete()) {
87             try {
88                 HttpServletRequestImpl request = servletRequestContext.getOriginalRequest();
89                 request.closeAndDrainRequest();
90             } finally {
91                 HttpServletResponseImpl response = servletRequestContext.getOriginalResponse();
92                 response.closeStreamAndWriter();
93             }
94         } else {
95             try {
96             HttpServletRequestImpl request = servletRequestContext.getOriginalRequest();
97             request.freeResources();
98             } finally {
99                 HttpServletResponseImpl response = servletRequestContext.getOriginalResponse();
100                 response.freeResources();
101             }
102         }
103     }
104
105     @Override
106     public Receiver getReceiver() {
107         return new BlockingReceiverImpl(exchange, getInputStream());
108     }
109 }
110