4

I am looking for a method to create XWalkWebResourceResponse. My case is following.

I am creating custom XWalkResourceClient client that will intercept all network requests. All request that should be intercepted comes back as abstract wrapper - AppWebResourceResponse.

public final class CrossWebViewClient extends XWalkResourceClient {
    // other fields and constructor

    CrossWebResourceResponseMapper responseMapper;

    @Override
    public XWalkWebResourceResponse shouldInterceptLoadRequest(XWalkView view, XWalkWebResourceRequest request) {
        AppWebResourceRequest mappedRequest = requestMapper.toGenericRequest(request);
        AppWebResourceResponse interceptedResponse = requestInterceptor.shouldInterceptRequest(mappedRequest);
        if (interceptedResponse == null) {
            return super.shouldInterceptLoadRequest(view, request);
        }
        return responseMapper.toTargetResponse(interceptedResponse);
    }    
}

The last step. I must adapt AppWebResourceResponse to XWalkWebResourceResponse and this is where I have an issue.

public class CrossWebResourceResponseMapper implements AppWebResourceResponse.Mapper<XWalkWebResourceResponse>  {
    @Override
    public XWalkWebResourceResponse toTargetResponse(final AppWebResourceResponse appResponse) {
        String mimeType = appResponse.getMimeType();
        String encoding = appResponse.getEncoding();
        int statusCode = appResponse.getStatusCode();
        String reasonPhrase = appResponse.getReasonPhrase();
        Map<String, String> headers = appResponse.getResponseHeaders();
        InputStream data = appResponse.getData();


        // ??? how to create response object?
        XWalkWebResourceResponse response;

        response.setMimeType(mimeType);
        response.setEncoding(encoding);
        response.setStatusCodeAndReasonPhrase(statusCode, reasonPhrase);
        response.setResponseHeaders(headers);
        response.setData(data);
        return response;
    }
}

According to API has public constructor new XWalkWebResourceResponse(bridge) that consumes bridge object, though I have failed to find a way to retrieve particular bridge(org.xwalk.core.internal.XWalkWebResourceResponseBridge).

Where can I retrieve XWalkWebResourceResponseBridge?

Thanks, in advance.

Update on 25/04/2016:

According to Xiaofeng it is possible to create XWalkWebResourceResponse object using XWalkResourceClient object.

public class XWalkWebResourceResponseMapper {
    public XWalkWebResourceResponse toTargetResponse(XWalkResourceClient client, AppWebResourceResponse appResponse) {
        String mimeType = appResponse.getMimeType();
        String encoding = appResponse.getEncoding();
        int statusCode = appResponse.getStatusCode();
        String reasonPhrase = appResponse.getReasonPhrase();
        Map<String, String> headers = appResponse.getResponseHeaders();
        InputStream data = appResponse.getData();

        return client.createXWalkWebResourceResponse(mimeType, encoding, data, statusCode, reasonPhrase, headers);
    }
}

public final class CrossWebViewClient extends XWalkResourceClient {
    @Override
    public XWalkWebResourceResponse shouldInterceptLoadRequest(XWalkView view, XWalkWebResourceRequest request) {
        AppWebResourceRequest mappedRequest = requestMapper.toGenericRequest(request);
        AppWebResourceResponse interceptedResponse = requestInterceptor.shouldInterceptRequest(mappedRequest);
        if (interceptedResponse == null) {
            return super.shouldInterceptLoadRequest(view, request);
        }
        return responseMapper.toTargetResponse(this, interceptedResponse);
    }
}
Community
  • 1
  • 1
Tom Koptel
  • 309
  • 1
  • 9

1 Answers1

4

Thanks using Crosswalk.

To create XWalkWebResourceResponse, you should use createXWalkWebResourceResponse in XWalkResourceClient, please see here. Because an internal block problem, it can't use general constructor to create.

Here is a sample about how to use it: https://github.com/crosswalk-project/crosswalk/pull/3454/files

 class MyResourceClient extends XWalkResourceClientInternal {
 MyResourceClient(XWalkViewInternal view) {
     super(view);
 }
 @Override
 XWalkWebResourceResponseInternal shouldInterceptLoadRequest(XWalkViewInternal view,
         XWalkWebResourceRequestInternal request) {
     // Handle it here.
     // Use createXWalkWebResourceResponse instead of "new XWalkWebResourceResponse"
     // to create the response.
     // Similar with before, there are two function to use:
     // 1) createXWalkWebResourceResponse(String mimeType, String encoding, InputStream data)
     // 2) createXWalkWebResourceResponse(String mimeType, String encoding, InputStream data,
     //             int statusCode, String reasonPhrase, Map<String, String> responseHeaders)
     ...
 }

}

You can also see this in XWalkView API doc later.

Hill
  • 379
  • 3
  • 9
  • 2
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/12132999) – Mayank Patel Apr 25 '16 at 04:07
  • @MayankPatel, thanks your reminding, but why you give a "not useful"? – Hill Apr 25 '16 at 08:38