1 package org
.argeo
.cms
.http
;
3 import java
.util
.ArrayList
;
4 import java
.util
.Collections
;
6 import java
.util
.Locale
;
7 import java
.util
.Objects
;
9 import org
.argeo
.cms
.auth
.RemoteAuthRequest
;
10 import org
.argeo
.cms
.auth
.RemoteAuthResponse
;
11 import org
.argeo
.cms
.auth
.RemoteAuthSession
;
13 import com
.sun
.net
.httpserver
.HttpExchange
;
16 * Implementation of {@link RemoteAuthRequest} and {@link RemoteAuthResponse}
17 * based on {@link HttpExchange}.
19 public class RemoteAuthHttpExchange
implements RemoteAuthRequest
, RemoteAuthResponse
{
20 private final HttpExchange httpExchange
;
21 private RemoteAuthSession remoteAuthSession
;
23 public RemoteAuthHttpExchange(HttpExchange httpExchange
) {
24 this.httpExchange
= httpExchange
;
25 this.remoteAuthSession
= (RemoteAuthSession
) httpExchange
.getAttribute(RemoteAuthSession
.class.getName());
26 Objects
.requireNonNull(this.remoteAuthSession
);
30 public void setHeader(String headerName
, String value
) {
31 httpExchange
.getResponseHeaders().put(headerName
, Collections
.singletonList(value
));
35 public void addHeader(String headerName
, String value
) {
36 List
<String
> values
= httpExchange
.getResponseHeaders().getOrDefault(headerName
, new ArrayList
<>());
41 public RemoteAuthSession
getSession() {
42 return remoteAuthSession
;
46 public RemoteAuthSession
createSession() {
47 throw new UnsupportedOperationException("Cannot create remote session");
51 public Locale
getLocale() {
52 // TODO Auto-generated method stub
57 public Object
getAttribute(String key
) {
58 return httpExchange
.getAttribute(key
);
62 public void setAttribute(String key
, Object object
) {
63 httpExchange
.setAttribute(key
, object
);
67 public String
getHeader(String key
) {
68 List
<String
> lst
= httpExchange
.getRequestHeaders().get(key
);
69 if (lst
== null || lst
.size() == 0)
75 public int getLocalPort() {
76 return httpExchange
.getLocalAddress().getPort();
80 public String
getRemoteAddr() {
81 return httpExchange
.getRemoteAddress().getHostName();
85 public int getRemotePort() {
86 return httpExchange
.getRemoteAddress().getPort();