Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2014 Red Hat, Inc., and individual contributors
   * as indicated by the @author tags.
   *
   * 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 io.undertow.websockets.spi;
 
 import org.xnio.Pool;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;

Author(s):
Stuart Douglas
 
 
     private final HttpServerExchange exchange;
     private Sender sender;
     private final Set<WebSocketChannelpeerConnections;
 
     public AsyncWebSocketHttpServerExchange(final HttpServerExchange exchangeSet<WebSocketChannelpeerConnections) {
         this. = exchange;
         this. = peerConnections;
     }
 
 
     @Override
     public <T> void putAttachment(final AttachmentKey<T> keyfinal T value) {
         .putAttachment(keyvalue);
     }
 
     @Override
     public <T> T getAttachment(final AttachmentKey<T> key) {
         return .getAttachment(key);
     }
 
     @Override
     public String getRequestHeader(final String headerName) {
         return .getRequestHeaders().getFirst(HttpString.tryFromString(headerName));
     }
 
     @Override
     public Map<StringList<String>> getRequestHeaders() {
         Map<StringList<String>> headers = new TreeMap<>(.);
         for (final HttpString header : .getRequestHeaders().getHeaderNames()) {
             headers.put(header.toString(), new ArrayList<>(.getRequestHeaders().get(header)));
         }
         return Collections.unmodifiableMap(headers);
     }
 
     @Override
     public String getResponseHeader(final String headerName) {
         return .getResponseHeaders().getFirst(HttpString.tryFromString(headerName));
     }
 
    @Override
    public Map<StringList<String>> getResponseHeaders() {
        Map<StringList<String>> headers = new HashMap<>();
        for (final HttpString header : .getResponseHeaders().getHeaderNames()) {
            headers.put(header.toString(), new ArrayList<>(.getResponseHeaders().get(header)));
        }
        return Collections.unmodifiableMap(headers);
    }
    @Override
    public void setResponseHeaders(final Map<StringList<String>> headers) {
        HeaderMap map = .getRequestHeaders();
        map.clear();
        for (Map.Entry<StringList<String>> header : headers.entrySet()) {
            map.addAll(HttpString.tryFromString(header.getKey()), header.getValue());
        }
    }
    @Override
    public void setResponseHeader(final String headerNamefinal String headerValue) {
        .getResponseHeaders().put(HttpString.tryFromString(headerName), headerValue);
    }
    @Override
    public void upgradeChannel(final HttpUpgradeListener upgradeCallback) {
        .upgradeChannel(upgradeCallback);
    }
    @Override
    public IoFuture<VoidsendData(final ByteBuffer data) {
        if ( == null) {
            this. = .getResponseSender();
        }
        final FutureResult<Voidfuture = new FutureResult<>();
        .send(datanew IoCallback() {
            @Override
            public void onComplete(final HttpServerExchange exchangefinal Sender sender) {
                future.setResult(null);
            }
            @Override
            public void onException(final HttpServerExchange exchangefinal Sender senderfinal IOException exception) {
                ..ioException(exception);
                future.setException(exception);
            }
        });
        return future.getIoFuture();
    }
    @Override
    public IoFuture<byte[]> readRequestData() {
        final ByteArrayOutputStream data = new ByteArrayOutputStream();
        final Pooled<ByteBufferpooled = .getConnection().getBufferPool().allocate();
        final ByteBuffer buffer = pooled.getResource();
        final StreamSourceChannel channel = .getRequestChannel();
        int res;
        for (; ; ) {
            try {
                res = channel.read(buffer);
                if (res == -1) {
                    return new FinishedIoFuture<>(data.toByteArray());
                } else if (res == 0) {
                    //callback
                    final FutureResult<byte[]> future = new FutureResult<>();
                    channel.getReadSetter().set(new ChannelListener<StreamSourceChannel>() {
                        @Override
                        public void handleEvent(final StreamSourceChannel channel) {
                            int res;
                            try {
                                res = channel.read(buffer);
                                if (res == -1) {
                                    future.setResult(data.toByteArray());
                                    channel.suspendReads();
                                    return;
                                } else if (res == 0) {
                                    return;
                                } else {
                                    buffer.flip();
                                    while (buffer.hasRemaining()) {
                                        data.write(buffer.get());
                                    }
                                    buffer.clear();
                                }
                            } catch (IOException e) {
                                future.setException(e);
                            }
                        }
                    });
                    channel.resumeReads();
                    return future.getIoFuture();
                } else {
                    buffer.flip();
                    while (buffer.hasRemaining()) {
                        data.write(buffer.get());
                    }
                    buffer.clear();
                }
            } catch (IOException e) {
                final FutureResult<byte[]> future = new FutureResult<>();
                future.setException(e);
                return future.getIoFuture();
            }
        }
    }
    @Override
    public void endExchange() {
        .endExchange();
    }
    @Override
    public void close() {
        try {
            .endExchange();
        } finally {
            IoUtils.safeClose(.getConnection());
        }
    }
    @Override
    public String getRequestScheme() {
        return .getRequestScheme();
    }
    @Override
    public String getRequestURI() {
        String q = .getQueryString();
        if (q == null || q.isEmpty()) {
            return .getRequestURI();
        } else {
            return .getRequestURI() + "?" + q;
        }
    }
    @Override
    public Pool<ByteBuffergetBufferPool() {
        return .getConnection().getBufferPool();
    }
    @Override
    public String getQueryString() {
        return .getQueryString();
    }
    @Override
    public Object getSession() {
        SessionConfig sessionCookieConfig = .getAttachment(.);
        if(sm != null && sessionCookieConfig != null) {
            return sm.getSession(sessionCookieConfig);
        }
        return null;
    }
    @Override
    public Map<StringList<String>> getRequestParameters() {
        Map<StringList<String>> params = new HashMap<>();
        for (Map.Entry<StringDeque<String>> param : .getQueryParameters().entrySet()) {
            params.put(param.getKey(), new ArrayList<>(param.getValue()));
        }
        return params;
    }
    @Override
    public Principal getUserPrincipal() {
        if(sc == null) {
            return null;
        }
        Account authenticatedAccount = sc.getAuthenticatedAccount();
        if(authenticatedAccount == null) {
            return null;
        }
        return authenticatedAccount.getPrincipal();
    }
    @Override
    public boolean isUserInRole(String role) {
        if(sc == null) {
            return false;
        }
        Account authenticatedAccount = sc.getAuthenticatedAccount();
        if(authenticatedAccount == null) {
            return false;
        }
        return authenticatedAccount.getRoles().contains(role);
    }
    @Override
        return ;
    }
New to GrepCode? Check out our FAQ X