use of javax.ws.rs.core.Cookie in project jersey by jersey.
the class FormResource method processForm.
/**
* Process the form submission. Produces a table showing the form field
* values submitted.
* @return a dynamically generated HTML table.
* @param formData the data from the form submission
*/
@POST
@Consumes("application/x-www-form-urlencoded")
public String processForm(MultivaluedMap<String, String> formData) {
StringBuilder buf = new StringBuilder();
buf.append("<html><head><title>Form results</title></head><body>");
buf.append("<p>Hello, you entered the following information: </p><table border='1'>");
for (String key : formData.keySet()) {
if (key.equals("submit")) {
continue;
}
buf.append("<tr><td>");
buf.append(key);
buf.append("</td><td>");
buf.append(formData.getFirst(key));
buf.append("</td></tr>");
}
for (Cookie c : headers.getCookies().values()) {
buf.append("<tr><td>Cookie: ");
buf.append(c.getName());
buf.append("</td><td>");
buf.append(c.getValue());
buf.append("</td></tr>");
}
buf.append("</table></body></html>");
return buf.toString();
}
use of javax.ws.rs.core.Cookie in project keywhiz by square.
the class CookieRenewingFilter method filter.
/**
* If the user has a valid session token, set a new session token. The new one should have a later
* expiration time.
*/
@Override
public void filter(ContainerRequestContext request, ContainerResponseContext response) throws IOException {
String sessionCookieName = sessionCookieConfig.getName();
// If the response will be setting a session cookie, don't overwrite it; just let it go.
if (response.getCookies().containsKey(sessionCookieName)) {
return;
}
// If the request doesn't have a session cookie, we're not going to renew one.
if (!request.getCookies().containsKey(sessionCookieName)) {
return;
}
Cookie requestCookie = request.getCookies().get(sessionCookieName);
Optional<User> optionalUser = authenticator.authenticate(requestCookie);
if (optionalUser.isPresent()) {
sessionLoginResource.cookiesForUser(optionalUser.get()).forEach(c -> response.getHeaders().add(HttpHeaders.SET_COOKIE, c));
}
}
use of javax.ws.rs.core.Cookie in project ORCID-Source by ORCID.
the class SwaggerJSONResource method getCookies.
protected Map<String, String> getCookies(HttpHeaders headers) {
Map<String, String> output = new HashMap<String, String>();
if (headers != null) {
for (String key : headers.getCookies().keySet()) {
Cookie cookie = headers.getCookies().get(key);
output.put(key, cookie.getValue());
}
}
return output;
}
use of javax.ws.rs.core.Cookie in project OpenAM by OpenRock.
the class MultipleResourceRestTest method setup.
@BeforeClass
public void setup() throws Exception {
PrivilegeManager pm = PrivilegeManager.getInstance(REALM, adminSubject);
{
Privilege privilege = Privilege.getNewInstance();
privilege.setName(PRIVILEGE_NAME + "1");
Map<String, Boolean> actions = new HashMap<String, Boolean>();
actions.put("GET", true);
Entitlement entitlement = new Entitlement(RESOURCE_NAME + "/*", actions);
privilege.setEntitlement(entitlement);
EntitlementSubject sbj = new AuthenticatedUsers();
privilege.setSubject(sbj);
pm.add(privilege);
}
{
Privilege privilege = Privilege.getNewInstance();
privilege.setName(PRIVILEGE_NAME + "2");
Map<String, Boolean> actions = new HashMap<String, Boolean>();
actions.put("GET", false);
Entitlement entitlement = new Entitlement(RESOURCE_NAME + "/index.html", actions);
privilege.setEntitlement(entitlement);
EntitlementSubject sbj = new AuthenticatedUsers();
privilege.setSubject(sbj);
pm.add(privilege);
}
String tokenId = adminToken.getTokenID().toString();
hashedTokenId = Hash.hash(tokenId);
tokenIdHeader = RestServiceManager.SSOTOKEN_SUBJECT_PREFIX + RestServiceManager.SUBJECT_DELIMITER + tokenId;
String cookieValue = tokenId;
if (Boolean.parseBoolean(SystemProperties.get(Constants.AM_COOKIE_ENCODE, "false"))) {
cookieValue = URLEncoder.encode(tokenId, "UTF-8");
}
cookie = new Cookie(SystemProperties.get(Constants.AM_COOKIE_NAME), cookieValue);
user = IdRepoUtils.createUser(REALM, "MultipleResourceRestTestUser");
decisionsClient = Client.create().resource(SystemProperties.getServerInstanceName() + "/ws/1/entitlement/decisions");
entitlementsClient = Client.create().resource(SystemProperties.getServerInstanceName() + "/ws/1/entitlement/entitlements");
}
use of javax.ws.rs.core.Cookie in project ats-framework by Axway.
the class RestClient method constructInvocationBuilder.
private Invocation.Builder constructInvocationBuilder(String descriptionToken) {
if (StringUtils.isNullOrEmpty(this.uri)) {
throw new IllegalArgumentException("Null or empty target URI. Please specify a valid one");
}
URL url;
try {
url = new URL(this.uri);
} catch (MalformedURLException e) {
throw new IllegalArgumentException("Please specify a valid URI. You have provided '" + this.uri + "'");
}
// remember these client id keys in order to work around a Jersey memory leak
List<String> clientIdKeys = new ArrayList<String>();
clientIdKeys.add("host_base_path=" + url.getProtocol() + "://" + url.getHost() + ":" + url.getPort());
// create the client builder
ClientBuilder clientBuilder = ClientBuilder.newBuilder();
if (this.uri.startsWith("https")) {
// configure Trust-all SSL context
SSLContext sslContext = SslUtils.getSSLContext(clientConfigurator.getCertificateFileName(), clientConfigurator.getCertificateFilePassword(), supportedProtocols[0]);
clientBuilder = clientBuilder.sslContext(sslContext).hostnameVerifier(new SslUtils.DefaultHostnameVerifier());
clientIdKeys.add("cert_file=" + clientConfigurator.getCertificateFileName());
}
// (e.g. features or individual entity providers, filters or interceptors)
for (Object provider : clientConfigurator.getProviders()) {
clientBuilder.register(provider);
clientIdKeys.add(provider.getClass().getName());
}
for (Class<?> providerClass : clientConfigurator.getProviderClasses()) {
clientBuilder.register(providerClass);
clientIdKeys.add(providerClass.getName());
}
// attach any configuration properties
Map<String, Object> properties = clientConfigurator.getProperties();
for (Entry<String, Object> propertyEntry : properties.entrySet()) {
clientBuilder.property(propertyEntry.getKey(), propertyEntry.getValue());
clientIdKeys.add(propertyEntry.getKey() + "=" + propertyEntry.getValue());
}
// basic authorization
if (username != null) {
clientIdKeys.add("user=" + username);
clientIdKeys.add("password=" + password);
clientBuilder.register(HttpAuthenticationFeature.basic(username, password));
}
// now create the client
client = getClient(clientIdKeys, clientBuilder);
if (debugLevel != RESTDebugLevel.NONE) {
RequestFilter requestFilter = new RequestFilter();
requestFilter.setDebugLevel(debugLevel);
client.register(requestFilter);
}
WebTarget webTarget = client.target(this.uri);
// navigate to internal resource
for (String token : resourcePath) {
webTarget = webTarget.path(token);
}
// add request parameters
for (Entry<String, List<String>> requestParamEntry : requestParameters.entrySet()) {
for (String requestParamValue : requestParamEntry.getValue()) {
webTarget = webTarget.queryParam(requestParamEntry.getKey(), requestParamValue);
}
}
log.info("We will " + descriptionToken + " " + webTarget.getUri());
Invocation.Builder invocationBuilder = webTarget.request();
// set response media type
if (!StringUtils.isNullOrEmpty(responseMediaType)) {
invocationBuilder = invocationBuilder.accept(RestMediaType.toMediaType(responseMediaType, responseMediaCharset));
}
// add request headers
for (Entry<String, Object> requestHeaderEntry : requestHeaders.entrySet()) {
invocationBuilder.header(requestHeaderEntry.getKey(), requestHeaderEntry.getValue());
}
// add request cookies
for (Cookie cookie : cookies) {
invocationBuilder.cookie(cookie);
}
return invocationBuilder;
}
Aggregations