Thursday, January 19, 2012

RIDC : How to Fetch the PDF from UCM & Display on the JSP page?

ADD RIDC REQUIRED JAR FILES:
---------------------------
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;


import java.io.PrintWriter;

import javax.servlet.*;
import javax.servlet.http.*;

import oracle.stellent.ridc.IdcClient;
import oracle.stellent.ridc.IdcClientException;
import oracle.stellent.ridc.IdcClientManager;
import oracle.stellent.ridc.IdcContext;
import oracle.stellent.ridc.model.DataBinder;
import oracle.stellent.ridc.protocol.ServiceResponse;

public class RIDCServlet extends HttpServlet {


static private String contentServerURL;

public void init(ServletConfig config) throws ServletException {
super.init(config);
if( (contentServerURL = getInitParameter("contentServerURL")) == null)
contentServerURL = "idc://localhost:4444";

}

public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {


//Check the Session validation.
HttpSession session =request.getSession(false);
if(session == null){
PrintWriter out = response.getWriter();
out.println("Your Session is Invalid, Please Login to WorkList.");

return;
}
String ContentId = request.getParameter("contentId");
if(ContentId.equals(null)){
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("ContentId which is Coming from UCM, is NULL.");
return;
}
RIDCCall(ContentId, request, response);

}

public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {

//Check the Session validation.
HttpSession session =request.getSession(false);
if(session == null){
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Your Session is Invalid, Please Login to WorkList.");

return;
}

String ContentId = request.getParameter("contentId");
if(ContentId.equals(null)){
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("ContentId which is Coming from UCM, is NULL.");
return;
}
RIDCCall(ContentId, request, response);
}

public static InputStream getInputStreamFromRIDC(String contentId) throws IdcClientException,
IOException {

// Arguments needed to connect to UCM
boolean dynamicConversion = false;
//String idcConnectionURL= "idc://localhost:4444";
String username;
username = "weblogic";
String password;
password = "welcome1";

// Create the IdcClient
IdcClientManager clientManager = new IdcClientManager();
IdcClient client = clientManager.createClient(contentServerURL);
IdcContext userContext = new IdcContext(username, password);

// Create a new binder
DataBinder dataBinder = client.createBinder();

// Retrieve the file
if (dynamicConversion) {
dataBinder.putLocal("IdcService", "GET_DYNAMIC_CONVERSION");
dataBinder.putLocal("IsJava", "0");
} else {
dataBinder.putLocal("IdcService", "GET_FILE");
}

dataBinder.putLocal("dDocName", contentId);
dataBinder.putLocal("RevisionSelectionMethod", "Latest");
//dataBinder.putLocal("Rendition","Web");
ServiceResponse ridc_response;
ridc_response = client.sendRequest(userContext, dataBinder);

return ridc_response.getResponseStream();
}

public void RIDCCall(String contentId, HttpServletRequest request,
HttpServletResponse response) {

response.reset();
response.setBufferSize(1024);
response.setContentType("application/pdf");

InputStream is = null;
try {
is = getInputStreamFromRIDC(contentId);
} catch (IdcClientException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

BufferedInputStream input = new BufferedInputStream(is);
BufferedOutputStream output;

// Write file contents to response.
try {
output = new BufferedOutputStream(response.getOutputStream());

byte[] buf = new byte[1024 * 256];
long i = 0;
int len;

while (true) {
i++;
System.out.println(i);
len = input.read(buf);
if (len == -1) {
break;
}
output.write(buf, 0, len);
}


} catch (IOException e) {
e.printStackTrace();
}

}
}