Giter Site home page Giter Site logo

Comments (5)

jansupol avatar jansupol commented on July 23, 2024

I believe this is one of the tasks that should have been solvable by HeaderDelegate and associated HeaderDelegateProvider SPI, so that each customer can specify whatever he desires. The SPI has been a bit overlooked in the past, but that's the idea.
Would it help in your case?

from jersey.

jluehe avatar jluehe commented on July 23, 2024

Thank you, @jansupol! That sounds like an interesting idea! Let me play with it and see if I can get it to work!

from jersey.

jluehe avatar jluehe commented on July 23, 2024

@jansupol, I was able to pinpoint where the whitespace gets stripped in Jersey 2:

  • Jersey 1.x:
package com.sun.jersey.core.impl.provider.header;

public class MediaTypeProvider implements HeaderDelegateProvider<MediaType> {

    @Override
    public String toString(MediaType header) {
        StringBuilder b = new StringBuilder();
        b.append(header.getType()).append('/').append(header.getSubtype());
        for (Map.Entry<String, String> e : header.getParameters().entrySet()) {
            b.append("; ").append(e.getKey()).append('=');    <<<<== whitespace added after semicolon
            WriterUtil.appendQuotedIfNonToken(b, e.getValue());
        }
        return b.toString();
    }
    
    ...
}
  • Jersey 2:

https://raw.githubusercontent.com/eclipse-ee4j/jersey/2.x/core-common/src/main/java/org/glassfish/jersey/message/internal/MediaTypeProvider.java:

package org.glassfish.jersey.message.internal;

public class MediaTypeProvider implements HeaderDelegateProvider<MediaType> {

    @Override
    public String toString(MediaType header) {

        throwIllegalArgumentExceptionIfNull(header, MEDIA_TYPE_IS_NULL);

        StringBuilder b = new StringBuilder();
        b.append(header.getType()).append('/').append(header.getSubtype());
        for (Map.Entry<String, String> e : header.getParameters().entrySet()) {
            b.append(";").append(e.getKey()).append('=');    <<<<== no more whitespace added after semicolon
            StringBuilderUtils.appendQuotedIfNonToken(b, e.getValue());
        }
        return b.toString();
    }

    ...
}

from jersey.

jluehe avatar jluehe commented on July 23, 2024

@jansupol, can you tell me how I can replace/override Jersey's MediaTypeProvider with my own?

I see Jersey's RuntimeDelegateImpl getting initialized as follows:

package org.glassfish.jersey.server.internal;

public RuntimeDelegateImpl() {
    super(new MessagingBinders.HeaderDelegateProviders().getHeaderDelegateProviders());
}

where the MessagingBinders.HeaderDelegateProviders constructor uses Jersey's built-in HeaderDelegateProviders:

        public HeaderDelegateProviders() {
            Set<HeaderDelegateProvider> providers = new HashSet<>();
            providers.add(new CacheControlProvider());
            providers.add(new CookieProvider());
            providers.add(new DateProvider());
            providers.add(new EntityTagProvider());
            providers.add(new LinkProvider());
            providers.add(new LocaleProvider());
            providers.add(new MediaTypeProvider());
            providers.add(new NewCookieProvider());
            providers.add(new StringHeaderProvider());
            providers.add(new UriProvider());
            this.providers = providers;
        }

I had hoped it would also consider any custom HeaderDelegateProviders declared in META-INF/services as described here: https://eclipse-ee4j.github.io/jersey.github.io/apidocs/2.34/jersey/org/glassfish/jersey/spi/HeaderDelegateProvider.html

Do you have any recommendation for how to make this work? I noticed the same question was asked here: https://stackoverflow.com/questions/62297701/how-to-inject-custom-implementation-of-org-glassfish-jersey-spi-headerdelegatepr, but it was never answered.

Thank you!

from jersey.

jluehe avatar jluehe commented on July 23, 2024

@jansupol, what I ended up doing is extend org.glassfish.jersey.server.internal.RuntimeDelegateImpl and have my subclass (declared in META-INF/services/javax.ws.rs.ext.RuntimeDelegate) override createHeaderDelegate ...

from jersey.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.