留言板

Empty file stream calling /api/secure/jsonws/dlfileentry/get-file-as-stream

Mdu Mdu,修改在11 年前。

Empty file stream calling /api/secure/jsonws/dlfileentry/get-file-as-stream

New Member 帖子: 12 加入日期: 05-10-31 最近的帖子
LF Info:
Version (Runtime and Source) : 6.1.1.ga2


Client Code Snippet:
		BasicHttpContext ctx = new BasicHttpContext();
		ctx.setAttribute(ClientContext.AUTH_CACHE, authCache);

		HttpPost post = new HttpPost("/api/secure/jsonws/dlfileentry/get-file-as-stream");
		List<namevaluepair> params = new ArrayList<namevaluepair>();
		params.add(new BasicNameValuePair("fileEntryId", "40522"));
		params.add(new BasicNameValuePair("version", "1.0"));

		UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
		post.setEntity(entity);

		HttpResponse resp = LiferayPortalDocumentRepositoryImpl.httpclient.execute(targetHost, post, ctx);
		System.out.println("getFolderById Status:[" + resp.getStatusLine() + "]");
		System.out.println("getFolderByName Res:[" + resp + "]");</namevaluepair></namevaluepair>


Result:
The response stream in HTTP reponse is empty or of length 0.

Notes:
- Returned JSON is null:
 json = getJSON(mapping, form, request, response); //com.liferay.portal.struts.JSONAction Line 75



- Returned result (FileInputStream) is NOT null:
Object returnObj = jsonWebServiceAction.invoke(); //com.liferay.portal.jsonwebservice.JSONWebServiceServiceAction Line 104


Question: How do we return file content?

Thanks.
varun pradeep shah,修改在11 年前。

RE: Empty file stream calling /api/secure/jsonws/dlfileentry/get-file-as-st

New Member 帖子: 5 加入日期: 08-11-28 最近的帖子
Hi ,
Did you get any solution? I am also facing same problem.
If you have any other solution then please share.

Thanks,
Varun Shah
Mdu Mdu,修改在11 年前。

RE: Empty file stream calling /api/secure/jsonws/dlfileentry/get-file-as-st

New Member 帖子: 12 加入日期: 05-10-31 最近的帖子
Varun - here's what we ended up doing. Probably a hack.

Code Base: liferay-portal-src-6.1.1-ce-ga2

Changed file: JSONWebServiceServiceAction.java:

ORIGINAL:

	@Override
	public String getJSON(
			ActionMapping actionMapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
		throws Exception {

		JSONWebServiceAction jsonWebServiceAction = null;

		String path = GetterUtil.getString(request.getPathInfo());

		try {
			if (path.equals("/invoke")) {
				jsonWebServiceAction = new JSONWebServiceInvokerAction(request);
			}
			else {
				jsonWebServiceAction =
					JSONWebServiceActionsManagerUtil.getJSONWebServiceAction(
						request);
			}

			JSONWebServiceActionMapping jsonWebServiceActionMapping =
				jsonWebServiceAction.getJSONWebServiceActionMapping();

			String actionMethodName = null;

			if (jsonWebServiceActionMapping != null) {
				Method actionMethod =
					jsonWebServiceActionMapping.getActionMethod();

				actionMethodName = actionMethod.getName();
			}

			checkMethodGuestAccess(
				request, actionMethodName,
				PropsValues.JSONWS_WEB_SERVICE_PUBLIC_METHODS);

			Object returnObj = jsonWebServiceAction.invoke();

			if (returnObj != null) {
				return getReturnValue(returnObj);
			}
			else {
				return JSONFactoryUtil.getNullJSON();
			}
		}
		catch (Exception e) {
			_log.error(e, e);

			return JSONFactoryUtil.serializeException(e);
		}
	}


MODIFIED:

	@Override
	public String getJSON(
			ActionMapping actionMapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
		throws Exception {

		JSONWebServiceAction jsonWebServiceAction = null;

		String path = GetterUtil.getString(request.getPathInfo());

		try {
			if (path.equals("/invoke")) {
				jsonWebServiceAction = new JSONWebServiceInvokerAction(request);
			}
			else {
				jsonWebServiceAction =
					JSONWebServiceActionsManagerUtil.getJSONWebServiceAction(
						request);
			}

			JSONWebServiceActionMapping jsonWebServiceActionMapping =
				jsonWebServiceAction.getJSONWebServiceActionMapping();

			String actionMethodName = null;

			if (jsonWebServiceActionMapping != null) {
				Method actionMethod =
					jsonWebServiceActionMapping.getActionMethod();

				actionMethodName = actionMethod.getName();
			}

			checkMethodGuestAccess(
				request, actionMethodName,
				PropsValues.JSONWS_WEB_SERVICE_PUBLIC_METHODS);

			Object returnObj = jsonWebServiceAction.invoke();
			
			if (returnObj instanceof FileInputStream)
			{
				FileInputStream fis = (FileInputStream)returnObj;
				response.setContentType("application/octet-stream");
				response.addHeader("Content-Type","application/octet-stream");
				response.setContentType("application/octet-stream");
				//response.setContentLength((int) pdfFile.length());

				OutputStream responseOutputStream = response.getOutputStream();
				int ttlBytes = (int)stream(fis,responseOutputStream);
	
				response.addHeader("Content-Length",Integer.toString(ttlBytes));
				response.setContentLength(ttlBytes);
				return "{filename:'file_entry_content'}";
			}
			else
			{
				if (returnObj != null) {
					return getReturnValue(returnObj);
				}
				else {
					return JSONFactoryUtil.getNullJSON();
				}
			}
		}
		catch (Exception e) {
			_log.error(e, e);

			return JSONFactoryUtil.serializeException(e);
		}
	}

	public static long stream(InputStream input, OutputStream output) throws IOException {
	    ReadableByteChannel inputChannel = null;
	    WritableByteChannel outputChannel = null;
	    try {
	        inputChannel = Channels.newChannel(input);
	        outputChannel = Channels.newChannel(output);
	        ByteBuffer buffer = ByteBuffer.allocate(10240);
	        long size = 0;

	        while (inputChannel.read(buffer) != -1) {
	            buffer.flip();
	            size += outputChannel.write(buffer);
	            buffer.clear();
	        }

	        return size;
	    }
	    finally {
	        if (outputChannel != null) try { outputChannel.close(); } catch (IOException ignore) { /**/ }
	        if (inputChannel != null) try { inputChannel.close(); } catch (IOException ignore) { /**/ }
	    }
	}	


I hope this helps. I welcome a proper way of fixing this. But if this solution is acceptable, we can also create a patch.