Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirect GuardianList#toString to original toString #5335

Merged
merged 1 commit into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,24 @@ public List<V> subList(int fromIndex, int toIndex) {
final List<V> sublist = guarded.subList(fromIndex, toIndex);
return sublist != null ? new GuardianList<>(sublist, guard) : sublist;
}

@Override
public String toString() {
return guarded.toString();
}

@Override
public boolean equals(Object obj) {
if (GuardianList.class.isInstance(obj)) {
return guarded.equals(((GuardianList) obj).guarded);
}
return guarded.equals(obj);
}

@Override
public int hashCode() {
return guarded.hashCode();
}
}

private static class GuardianIterator<V> extends MutableGuardian<V> implements Iterator<V> {
Expand Down Expand Up @@ -411,6 +429,11 @@ public void remove() {
public void forEachRemaining(Consumer<? super V> action) {
guarded.forEachRemaining(action);
}

@Override
public String toString() {
return guarded.toString();
}
}

private static class GuardianListIterator<V> extends GuardianIterator<V> implements ListIterator<V> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -16,8 +16,10 @@

package org.glassfish.jersey.tests.e2e;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
Expand All @@ -32,12 +34,14 @@
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Variant;

import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand Down Expand Up @@ -80,6 +84,30 @@ public String contentHeaders(@HeaderParam("custom-header") final String customHe

return sb.toString();
}

@GET
@Path("/tostring")
public String headersGet(@Context HttpHeaders hs) {
StringBuilder sb = new StringBuilder();
List<String> myHeaders = Arrays.asList("Accept", "Content-Type");

try {
MultivaluedMap<String, String> rqhdrs = hs.getRequestHeaders();
Set<String> keys = rqhdrs.keySet();
sb.append("getRequestHeaders= ");
for (String header : myHeaders) {
if (keys.contains(header)) {
sb.append(
"Found " + header + ": " + hs.getRequestHeader(header) + "; ");
}
}
} catch (Throwable ex) {
sb.append("Unexpected exception thrown in getRequestHeaders: "
+ ex.getMessage());
ex.printStackTrace();
}
return sb.toString();
}
}

@Override
Expand All @@ -97,6 +125,26 @@ public void testAccesingHelloworldResource() {
assertEquals(HelloWorldResource.MESSAGE, responseMessage);
}

@Test
public void testHeadersToString() {
try (Response response = target("headers").path("tostring").request()
.header(HttpHeaders.ACCEPT, "text/*, text/html, text/html;level=1, */*")
.header(HttpHeaders.CONTENT_TYPE, "application/xml;charset=utf8")
.get()) {
String content = response.readEntity(String.class);
int index = -1;
Assertions.assertTrue((index = content.indexOf("getRequestHeaders=")) != -1);
Assertions.assertTrue((index = content.indexOf("Accept:")) != -1);
Assertions.assertTrue((index = content.indexOf("text/*")) != -1);
Assertions.assertTrue((index = content.indexOf("text/html")) != -1);
Assertions.assertTrue((index = content.indexOf("text/html")) != -1);
Assertions.assertTrue((index = content.indexOf("*/*")) != -1);
Assertions.assertTrue((index = content.indexOf("Content-Type:")) != -1);
Assertions.assertTrue((index = content.indexOf("application/xml")) != -1);
Assertions.assertTrue((index = content.indexOf("charset=utf8")) != -1);
}
}

@Test
public void testAccesingMissingResource() {
final WebTarget missingResource = target().path("missing");
Expand Down