-
Notifications
You must be signed in to change notification settings - Fork 2
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
Support to use application-fury in resteasy #26
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package io.quarkiverse.fury; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Type; | ||
|
||
import jakarta.ws.rs.Consumes; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.WebApplicationException; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.MultivaluedMap; | ||
import jakarta.ws.rs.ext.MessageBodyReader; | ||
import jakarta.ws.rs.ext.MessageBodyWriter; | ||
import jakarta.ws.rs.ext.Provider; | ||
|
||
import org.apache.fury.BaseFury; | ||
import org.apache.fury.Fury; | ||
import org.apache.fury.ThreadSafeFury; | ||
import org.apache.fury.io.FuryInputStream; | ||
import org.apache.fury.resolver.ClassResolver; | ||
|
||
import io.quarkus.arc.Arc; | ||
|
||
@Provider | ||
@Consumes({ "application/fury", "application/*+fury" }) | ||
@Produces({ "application/fury", "application/*+fury" }) | ||
public class ClassicFurySerializer implements MessageBodyReader<Object>, MessageBodyWriter<Object> { | ||
private BaseFury fury; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ha, it shoudl work. Let me try. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, it does not work. Because resteasy will init the providers at static_init time where the bean container is not running. We have to retrieve |
||
|
||
public ClassicFurySerializer() { | ||
} | ||
|
||
@Override | ||
public boolean isReadable(final Class<?> aClass, final Type type, final Annotation[] annotations, | ||
final MediaType mediaType) { | ||
return isSupportedMediaType(mediaType) && canSerialize(aClass); | ||
} | ||
|
||
@Override | ||
public Object readFrom(final Class<Object> aClass, final Type type, final Annotation[] annotations, | ||
final MediaType mediaType, final MultivaluedMap<String, String> multivaluedMap, final InputStream inputStream) | ||
throws WebApplicationException { | ||
return getFury().deserialize(new FuryInputStream(inputStream)); | ||
} | ||
|
||
@Override | ||
public boolean isWriteable(final Class<?> aClass, final Type type, final Annotation[] annotations, | ||
final MediaType mediaType) { | ||
return isSupportedMediaType(mediaType) && canSerialize(aClass); | ||
} | ||
|
||
@Override | ||
public void writeTo(final Object obj, final Class<?> aClass, final Type type, final Annotation[] annotations, | ||
final MediaType mediaType, final MultivaluedMap<String, Object> multivaluedMap, final OutputStream outputStream) | ||
throws IOException, WebApplicationException { | ||
outputStream.write(getFury().serialize(obj)); | ||
} | ||
|
||
private boolean isSupportedMediaType(MediaType mediaType) { | ||
return mediaType.getType().equals("application") && mediaType.getSubtype().endsWith("fury"); | ||
} | ||
|
||
private boolean canSerialize(final Class<?> aClass) { | ||
if (getFury() instanceof final ThreadSafeFury threadSafeFury) { | ||
return (threadSafeFury).execute(f -> f.getClassResolver().getRegisteredClassId(aClass)) != null; | ||
} else { | ||
ClassResolver classResolver = ((Fury) getFury()).getClassResolver(); | ||
return classResolver.getRegisteredClassId(aClass) != null; | ||
} | ||
} | ||
|
||
private BaseFury getFury() { | ||
if (fury == null) { | ||
fury = Arc.container().instance(BaseFury.class).get(); | ||
} | ||
return fury; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.quarkiverse.fury; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.context.Dependent; | ||
import jakarta.enterprise.inject.Produces; | ||
|
||
@Dependent | ||
public class ClassicFurySerializerProducer { | ||
@Produces | ||
@ApplicationScoped | ||
public ClassicFurySerializer create() { | ||
return new ClassicFurySerializer(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't write code like this, the serialization and deserialization should have same class registration order. If you register Bar here, it allocate an ID x for Bar, but the server allocate id x for ThirdPartyBar. This is why it fails. You could update like this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's the root cause. Thanks a lot @chaokunyang ! Should we need a NOTE in the documentation or refer to the Apache Fury docs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, would you like submit a pr?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean a PR for
Fury
?