Skip to content

Commit

Permalink
Additional modification for Jakarta REST 4.0 API
Browse files Browse the repository at this point in the history
Signed-off-by: jansupol <jan.supol@oracle.com>
  • Loading branch information
jansupol committed Mar 25, 2024
1 parent 06cddd9 commit be12d37
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,15 @@ public List<String> getMatchedURIs() {
//@Override
public String getMatchedResourceTemplate() {
final StringBuilder sb = new StringBuilder();
for (String template : getMatchedResourceTemplates()) {
sb.append(template.trim());
}
return sb.toString();
}

//@Override
public List<String> getMatchedResourceTemplates() {
final List<String> list = new ArrayList<>();
if (ResourceConfig.class.isInstance(requestContext.getConfiguration())) {
Application app = ((ResourceConfig) requestContext.getConfiguration()).getApplication();
while (ResourceConfig.class.isInstance(app) && ((ResourceConfig) app).getApplication() != app) {
Expand All @@ -259,18 +268,18 @@ public String getMatchedResourceTemplate() {
final ApplicationPath annotation = app.getClass().getAnnotation(ApplicationPath.class);
if (annotation != null) {
String value = annotation.value();
sb.append(value.endsWith("/") ? value.substring(0, value.length() - 1) : value);
list.add(value.endsWith("/") ? value.substring(0, value.length() - 1) : value);
}
}

final Iterator<UriTemplate> templateIt = templates.descendingIterator();
while (templateIt.hasNext()) {
String template = templateIt.next().getTemplate().trim();
if (!template.equals("/") || sb.isEmpty()) {
sb.append(template);
if (!template.equals("/") || list.isEmpty()) { // !subresourceLocator
list.add(template);
}
}
return sb.toString();
return list;
}

private static final Function<String, String> PATH_DECODER = input -> UriComponent.decode(input, UriComponent.Type.PATH);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
Expand Down Expand Up @@ -70,7 +70,7 @@ public class ItemRepresentation {
bindings = @Binding(name = "id", value = "${instance.id}"),
rel = "self"
)
@XmlJavaTypeAdapter(Link.JaxbAdapter.class)
@XmlJavaTypeAdapter(JaxbAdapter.class)
@XmlElement(name = "link")
Link self;

Expand All @@ -91,7 +91,7 @@ public class ItemRepresentation {
)})
@XmlElement(name = "link")
@XmlElementWrapper(name = "links")
@XmlJavaTypeAdapter(Link.JaxbAdapter.class)
@XmlJavaTypeAdapter(JaxbAdapter.class)
List<Link> links;

public ItemRepresentation() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
Expand Down Expand Up @@ -82,7 +82,7 @@ public class ItemsRepresentation {
},
rel = "self"
)
@XmlJavaTypeAdapter(Link.JaxbAdapter.class)
@XmlJavaTypeAdapter(JaxbAdapter.class)
@XmlElement(name = "link")
Link self;

Expand Down Expand Up @@ -111,7 +111,7 @@ public class ItemsRepresentation {
)})
@XmlElement(name = "link")
@XmlElementWrapper(name = "links")
@XmlJavaTypeAdapter(Link.JaxbAdapter.class)
@XmlJavaTypeAdapter(JaxbAdapter.class)
List<Link> links;

public ItemsRepresentation() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.glassfish.jersey.examples.linking.representation;

import jakarta.ws.rs.core.Link;
import jakarta.xml.bind.annotation.adapters.XmlAdapter;

import javax.xml.namespace.QName;
import java.util.Map;

/**
* An implementation of JAXB {@link XmlAdapter} that maps the JAX-RS
* {@link Link} type to a value that can be marshalled and unmarshalled by JAXB. The following example
* shows how to use this adapter on a JAXB bean class:
*
* <pre>
* &#64;XmlRootElement
* public class MyModel {
*
* private Link link;
*
* &#64;XmlElement(name="link")
* &#64;XmlJavaTypeAdapter(JaxbAdapter.class)
* public Link getLink() {
* return link;
* }
* ...
* }
* </pre>
*
* <p>
* Note that usage of this class requires the Jakarta XML Binding API and an implementation. The Jakarta RESTful Web
* Services implementation is not required to provide these dependencies.
* </p>
* <p>
* The class used to be a part Jakarta REST 3.1
* </p>
*
* @see JaxbLink
* @since 4.0
*/
public class JaxbAdapter extends XmlAdapter<JaxbLink, Link> {

/**
* Convert a {@link JaxbLink} into a {@link Link}.
*
* @param v instance of type {@link JaxbLink}.
* @return mapped instance of type {@link JaxbLink}
*/
@Override
public Link unmarshal(final JaxbLink v) {
Link.Builder lb = Link.fromUri(v.getUri());
for (Map.Entry<QName, Object> e : v.getParams().entrySet()) {
lb.param(e.getKey().getLocalPart(), e.getValue().toString());
}
return lb.build();
}

/**
* Convert a {@link Link} into a {@link JaxbLink}.
*
* @param v instance of type {@link Link}.
* @return mapped instance of type {@link JaxbLink}.
*/
@Override
public JaxbLink marshal(final Link v) {
JaxbLink jl = new JaxbLink(v.getUri());
for (Map.Entry<String, String> e : v.getParams().entrySet()) {
final String name = e.getKey();
jl.getParams().put(new QName("", name), e.getValue());
}
return jl;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* Copyright (c) 2024 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
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.examples.linking.representation;

import jakarta.xml.bind.annotation.XmlAnyAttribute;
import jakarta.xml.bind.annotation.XmlAttribute;

import javax.xml.namespace.QName;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* <p>
* Value type for {@link jakarta.ws.rs.core.Link} that can be marshalled and
* unmarshalled by JAXB.
* </p>
* <p>
* Note that usage of this class requires the Jakarta XML Binding API and an implementation. The Jakarta RESTful Web
* Services implementation is not required to provide these dependencies.
* </p>
* <p>
* The class used to be a part Jakarta REST API 3.1.
* </p>
*
* @see JaxbAdapter
* @since 4.0
*/
public class JaxbLink {

private URI uri;
private Map<QName, Object> params;

/**
* Default constructor needed during unmarshalling.
*/
public JaxbLink() {
}

/**
* Construct an instance from a URI and no parameters.
*
* @param uri underlying URI.
*/
public JaxbLink(final URI uri) {
this.uri = uri;
}

/**
* Construct an instance from a URI and some parameters.
*
* @param uri underlying URI.
* @param params parameters of this link.
*/
public JaxbLink(final URI uri, final Map<QName, Object> params) {
this.uri = uri;
this.params = params;
}

/**
* Get the underlying URI for this link.
*
* @return underlying URI.
*/
@XmlAttribute(name = "href")
public URI getUri() {
return uri;
}

/**
* Get the parameter map for this link.
*
* @return parameter map.
*/
@XmlAnyAttribute
public Map<QName, Object> getParams() {
if (params == null) {
params = new HashMap<QName, Object>();
}
return params;
}

/**
* Set the underlying URI for this link.
*
* This setter is needed for JAXB unmarshalling.
*/
void setUri(final URI uri) {
this.uri = uri;
}

/**
* Set the parameter map for this link.
*
* This setter is needed for JAXB unmarshalling.
*/
void setParams(final Map<QName, Object> params) {
this.params = params;
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (!(o instanceof JaxbLink)) {
return false;
}

JaxbLink jaxbLink = (JaxbLink) o;

if (uri != null ? !uri.equals(jaxbLink.uri) : jaxbLink.uri != null) {
return false;
}

if (params == jaxbLink.params) {
return true;
}
if (params == null) {
// if this.params is 'null', consider other.params equal to empty
return jaxbLink.params.isEmpty();
}
if (jaxbLink.params == null) {
// if other.params is 'null', consider this.params equal to empty
return params.isEmpty();
}

return params.equals(jaxbLink.params);
}

@Override
public int hashCode() {
return Objects.hash(uri, params);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2024 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 Down Expand Up @@ -147,6 +147,14 @@ public List<String> getMatchedURIs(boolean decode) {
throw new UnsupportedOperationException("Not supported yet.");
}

public String getMatchedResourceTemplate() {
throw new UnsupportedOperationException("Not supported yet.");
}

public List<String> getMatchedResourceTemplates() {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public List<Object> getMatchedResources() {
Object dummyResource = new Object() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2024 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 Down Expand Up @@ -114,6 +114,14 @@ public List<String> getMatchedURIs(boolean decode) {
throw new UnsupportedOperationException("Not supported yet.");
}

public List<String> getMatchedResourceTemplates() {
throw new UnsupportedOperationException("Not supported yet.");
}

public String getMatchedResourceTemplate() {
throw new UnsupportedOperationException("Not supported yet.");
}

public List<Object> getMatchedResources() {
Object dummyResource = new Object() {};
return Collections.singletonList(dummyResource);
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2296,7 +2296,7 @@
<jaxb.ri.version>4.0.4</jaxb.ri.version>
<jaxrs.api.spec.version>3.1</jaxrs.api.spec.version>
<jaxrs.api.impl.version>3.1.0</jaxrs.api.impl.version>
<jakarta.rest.osgi.version>jakarta.ws.rs;version="[3.1,5)",jakarta.ws.rs.client;version="[3.1,5)",jakarta.ws.rs.container;version="[3.1,5)",jakarta.ws.rs.core;version="[3.1,5)",jakarta.ws.rs.ext;version="[3.1,5)"</jakarta.rest.osgi.version>
<jakarta.rest.osgi.version>jakarta.ws.rs;version="[3.1,5)",jakarta.ws.rs.client;version="[3.1,5)",jakarta.ws.rs.container;version="[3.1,5)",jakarta.ws.rs.core;version="[3.1,5)",jakarta.ws.rs.ext;version="[3.1,5)",jakarta.ws.rs.sse;version="[3.1,5)"</jakarta.rest.osgi.version>
<jetty.osgi.version>org.eclipse.jetty.*;version="[11,15)"</jetty.osgi.version>
<jetty.version>12.0.7</jetty.version>
<jetty9.version>9.4.53.v20231009</jetty9.version>
Expand Down

0 comments on commit be12d37

Please sign in to comment.