I find one solution, implement my own ResponseContent with support for
Users Content-Length
thats the code
public class MyCustomResponseContent implements HttpResponseInterceptor
{
private final boolean overwrite;
/**
* Default constructor. The <code>Content-Length</code> or
<code>Transfer- Encoding</code>
* will cause the interceptor to throw {@link ProtocolException} if
already present in the
* response message.
*/
public MyCustomResponseContent()
{
this(false);
}
/**
* Constructor that can be used to fine-tune behavior of this
interceptor.
*
* @param overwrite If set to <code>true</code> the
<code>Content-Length</code> and
* <code>Transfer-Encoding</code> headers will be created or updated if
already present.
* If set to <code>false</code> the <code>Content-Length</code> and
* <code>Transfer-Encoding</code> headers will cause the interceptor to
throw
* {@link ProtocolException} if already present in the response message.
*
* @since 4.2
*/
public MyCustomResponseContent(boolean overwrite)
{
super();
this.overwrite = overwrite;
}
/**
* Processes the response (possibly updating or inserting)
Content-Length and Transfer-Encoding headers.
* @param response The HttpResponse to modify.
* @param context Unused.
* @throws ProtocolException If either the Content-Length or
Transfer-Encoding headers are found.
* @throws IllegalArgumentException If the response is null.
*/
public void process(final HttpResponse response, final HttpContext
context) throws HttpException, IOException
{
if (response == null)
{
throw new IllegalArgumentException("HTTP response may not be
null");
}
if (this.overwrite)
{
response.removeHeaders(HTTP.TRANSFER_ENCODING);
response.removeHeaders(HTTP.CONTENT_LEN);
}
else
{
if (response.containsHeader(HTTP.TRANSFER_ENCODING))
{
throw new ProtocolException("Transfer-encoding header
already present");
}
/*
if (response.containsHeader(HTTP.CONTENT_LEN))
{
throw new ProtocolException("Content-Length header already
present");
}
*/
}
ProtocolVersion ver =
response.getStatusLine().getProtocolVersion();
HttpEntity entity = response.getEntity();
if (entity != null)
{
long len = entity.getContentLength();
if (entity.isChunked() &&
!ver.lessEquals(HttpVersion.HTTP_1_0))
{
response.addHeader(HTTP.TRANSFER_ENCODING,
HTTP.CHUNK_CODING);
}
else if (len >= 0)
{
response.addHeader(HTTP.CONTENT_LEN,
Long.toString(entity.getContentLength()));
}
// Specify a content type if known
if (entity.getContentType() != null &&
!response.containsHeader(HTTP.CONTENT_TYPE ))
{
response.addHeader(entity.getContentType());
}
// Specify a content encoding if known
if (entity.getContentEncoding() != null &&
!response.containsHeader(HTTP.CONTENT_ENCODING))
{
response.addHeader(entity.getContentEncoding());
}
}
else if (!response.containsHeader(HTTP.CONTENT_LEN))
{
int status = response.getStatusLine().getStatusCode();
if (status != HttpStatus.SC_NO_CONTENT && status !=
HttpStatus.SC_NOT_MODIFIED && status != HttpStatus.SC_RESET_CONTENT)
{
response.addHeader(HTTP.CONTENT_LEN, "0");
}
}
}
}
El martes, 21 de agosto de 2012 12:31:32 UTC+2, Jose Luis Larraz escribió:
>
> Hello i had the next problem, i want to answer to a Head request with a
> Content-Length.
>
> I put an example
>
> HEAD /external/images/media/21.jpg HTTP/1.0
>
> getcontentFeatures.dlna.org: 1
>
> Host: 192.168.1.130:57645
>
> HTTP/1.1 200 OK
>
> Date: Tue, 21 Aug 2012 10:24:59 GMT
>
> Cache-Control: no-cache
>
> transferMode.dlna.org: Streaming
>
> contentFeatures.dlna.org:
> DLNA.ORG_PN=JPEG_LRG;DLNA.ORG_OP=01;DLNA.ORG_CI=0
>
> Content-Type: image/jpeg
>
> Last-Modified: Sat, 25 Feb 2012 15:11:58 GMT
>
> Content-Length: 60909
>
> Accept-Ranges: bytes
> ----------------------------
>
> The problem comes when i try to put Content-Length: 60909, I always get
> this exception "Content-Length header already present".
>
> This is my code
>
> public void handle(HttpRequest request,
> HttpResponse response,
> HttpContext context) throws HttpException,
> IOException
> {
> String method =
> request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH);
>
> if(method.equals("HEAD"))
> {
> MimeType mimeType = getMimeType(obj);
> long sizeInBytes = getSizeInBytes(obj);
>
>
>
> response.setHeader("Cache-control", "no-cache");
> response.setHeader("transferMode.dlna.org", "Streaming");
> String aMimeType = mimeType.toString();
> String dlnaspec="";
>
> if (aMimeType.equals("image/jpeg"))
> dlnaspec = "DLNA.ORG_PN=JPEG_LRG";
> else if (aMimeType.equals("audio/mpeg"))
> dlnaspec = "DLNA.ORG_PN=MP3";
> else if (aMimeType.equals("audio/L16") ||
> aMimeType.equals("audio/wav"))
> dlnaspec = "DLNA.ORG_PN=LPCM";
>
> response.setHeader("contentFeatures.dlna.org",
> dlnaspec+";DLNA.ORG_OP=01;DLNA.ORG_CI=0");
>
> response.setHeader("Content-Type", mimeType.toString());
> response.setHeader("Accept-Ranges", "bytes");
> response.setHeader("Content-Length",
> String.valueOf(sizeInBytes));
> response.setStatusCode(HttpStatus.SC_OK);
> }
> }
> -----------------
> I also have tried to put an empty entity in this way
>
> ByteArrayInputStream stream=new ByteArrayInputStream(new byte[(int)
> sizeInBytes]);
>
> InputStreamEntity entity = new InputStreamEntity(stream, sizeInBytes);
> response.setEntity(entity);
> but the problem is that a Head method dont have a body message so this
> approach is not valid.
>
> Any ideas?
>
>
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en