Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Implementing size() method. #674

Merged
merged 1 commit into from

2 participants

@cstamas
Owner

Default implementation was returning -1 constant for
getSize(), that made Jersey to stick with Chunked
transfer encoding (did not know exact size of http message body).

Some HTTP Servers does not like it, like nginx.

This is applied to all client subsystems.

Fixes:
jcabi/jcabi#47

@cstamas cstamas Implementing size() method.
Default implementation was returning -1 constant for
getSize(), that could explain why Jersey sticked to Chunked
transfer encoding (did not know exact size of http message body).
3456e20
@cstamas cstamas merged commit 7b37e04 into from
@rseddon
Owner

Why is there a 2nd marshal in the getSize method? Is there a way to only have one call?

@cstamas
Owner

writeTo and getSize are invoked at different times, and provider should be stateless, while Jersey API did not give me any "context" (like some place to stuff resulting XML payload and reuse the same in other method call).

so, yes, it is marshalled twice, as there is (am unaware) of any other solution to this, and to keep provider stateless.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 23, 2012
  1. @cstamas

    Implementing size() method.

    cstamas authored
    Default implementation was returning -1 constant for
    getSize(), that could explain why Jersey sticked to Chunked
    transfer encoding (did not know exact size of http message body).
This page is out of date. Refresh to see the latest.
View
25 nexus/nexus-client-core/src/main/java/org/sonatype/nexus/client/internal/rest/XStreamXmlProvider.java
@@ -12,11 +12,13 @@
*/
package org.sonatype.nexus.client.internal.rest;
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
+import java.io.UnsupportedEncodingException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import javax.ws.rs.WebApplicationException;
@@ -74,19 +76,38 @@ protected String getCharsetAsString( MediaType m )
return ( result == null ) ? DEFAULT_ENCODING : result;
}
+ @Override
public Object readFrom( Class<Object> aClass, Type genericType, Annotation[] annotations, MediaType mediaType,
- MultivaluedMap<String, String> map, InputStream stream )
+ MultivaluedMap<String, String> map, InputStream stream )
throws IOException, WebApplicationException
{
String encoding = getCharsetAsString( mediaType );
return xstream.fromXML( new InputStreamReader( stream, encoding ) );
}
+ @Override
public void writeTo( Object o, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType,
- MultivaluedMap<String, Object> map, OutputStream stream )
+ MultivaluedMap<String, Object> map, OutputStream stream )
throws IOException, WebApplicationException
{
String encoding = getCharsetAsString( mediaType );
xstream.marshal( o, new CompactWriter( new OutputStreamWriter( stream, encoding ) ) );
}
+
+ @Override
+ public long getSize( Object o, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType )
+ {
+ try
+ {
+ final ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ String encoding = getCharsetAsString( mediaType );
+ xstream.marshal( o, new CompactWriter( new OutputStreamWriter( bos, encoding ) ) );
+ return bos.size();
+ }
+ catch ( UnsupportedEncodingException e )
+ {
+ // huh?
+ return -1;
+ }
+ }
}
View
1  nexus/nexus-client-core/src/main/java/org/sonatype/nexus/client/rest/jersey/JerseyNexusClientFactory.java
@@ -24,7 +24,6 @@
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.conn.params.ConnRoutePNames;
-import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.CoreProtocolPNames;
import org.sonatype.nexus.client.core.Condition;
import org.sonatype.nexus.client.core.spi.SubsystemFactory;
Something went wrong with that request. Please try again.