I have built an export/import tool for Knowledgebase articles using Partner API.
Everything works fine, the only functionality I can't produce is the ability to export images, hence obviously I can't import the images.
After much reading on the subject I understand that I need to use HttpClient after giving the access token cookie, but when requesting in that manner the response comes back as 'Page Redirect' instead of returning the image.
Image url example, as it appears inside the rich text area field:
https://c.eu0.content.force.com/servlet/rtaImage?eid=ka3200000004MwH&feoid=00N20000008fSIK&refid=0EM20000000TzfR
Note: I'm also using the access token to connect to the partner API, it's works great.
My tool will be hosted on Heroku using servlets, this is a simple code just for trying to fetch the image:
OAuthServlet
public class OAuthServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
private static final String ACCESS_TOKEN = "ACCESS_TOKEN";
private static final String INSTANCE_URL = "INSTANCE_URL";
private String clientId = null;
private String clientSecret = null;
private String redirectUri = null;
private String environment = null;
private String authUrl = null;
private String tokenUrl = null;
public void init() throws ServletException
{
clientId = System.getenv("SFDC_OAUTH_CLIENT_ID");
clientSecret = System.getenv("SFDC_OAUTH_CLIENT_SECRET");
redirectUri = "http://localhost:8080/sfdc/callback.html";
environment = "https://login.salesforce.com";
try
{
authUrl = environment + "/services/oauth2/authorize?response_type=code&client_id=" + clientId + "&redirect_uri="
+ URLEncoder.encode(redirectUri, "UTF-8");
}
catch (UnsupportedEncodingException e)
{
throw new ServletException(e);
}
tokenUrl = environment + "/services/oauth2/token";
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String accessToken = (String) request.getSession().getAttribute(ACCESS_TOKEN);
if (accessToken == null)
{
String instanceUrl = null;
if (request.getRequestURI().endsWith("login"))
{
// we need to send the user to authorize
response.sendRedirect(authUrl);
return;
}
else
{
System.out.println("Auth successful - got callback");
String code = request.getParameter("code");
HttpClient httpclient = new HttpClient();
PostMethod post = new PostMethod(tokenUrl);
post.addParameter("code", code);
post.addParameter("grant_type", "authorization_code");
post.addParameter("client_id", clientId);
post.addParameter("client_secret", clientSecret);
post.addParameter("redirect_uri", redirectUri);
try
{
httpclient.executeMethod(post);
try
{
String responseBody = post.getResponseBodyAsString();
JSONObject authResponse = new JSONObject(responseBody);
System.out.println("Auth response: " + authResponse.toString(2));
accessToken = authResponse.getString("access_token");
instanceUrl = authResponse.getString("instance_url");
System.out.println("Got access token: " + accessToken);
Cookie session = new Cookie(ACCESS_TOKEN, accessToken);
session.setPath("/");
session.setMaxAge(-1); // cookie not persistent,
// destroyed on browser exit
response.addCookie(session);
}
catch (JSONException e)
{
e.printStackTrace();
throw new ServletException(e);
}
}
finally
{
post.releaseConnection();
}
}
// Set a session attribute so that other servlets can get the access
// token
request.getSession().setAttribute(ACCESS_TOKEN, accessToken);
// We also get the instance URL from the OAuth response, so set it
// in the session too
request.getSession().setAttribute(INSTANCE_URL, instanceUrl);
}
response.sendRedirect(request.getContextPath() + "/welcome");
}
WelcomeServlet
public class WelcomeServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
private static final String ACCESS_TOKEN = "ACCESS_TOKEN";
public void init() throws ServletException { }
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
// TODO Auto-generated method stub
String urlString = "https://c.eu0.content.force.com/servlet/rtaImage?eid=ka3200000004MwH&feoid=00N20000008fSIK&refid=0EM20000000TzfR";
HttpClient client = new HttpClient();
client.getParams().setParameter("http.protocol.single-cookie-header", true);
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
GetMethod method = new GetMethod();
String accessToken = (String) request.getSession().getAttribute(ACCESS_TOKEN);
method.setRequestHeader("Authorization", "Bearer " + accessToken);
method.setURI(new URI(urlString, true));
int returnCode = client.executeMethod(method);
if (returnCode != HttpStatus.SC_OK)
{
System.err.println("Unable to fetch image, status code: " + returnCode);
}
InputStream imageData = method.getResponseBodyAsStream();
OutputStream out = new BufferedOutputStream(new FileOutputStream("test-image.jpg"));
for (int i; (i = imageData.read()) != -1;)
{
out.write(i);
}
imageData.close();
out.close();
response.sendRedirect("welcome.jsp");
}
}
Thanks in advanced.