<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest posts for the topic "Making post from another tomcat application"]]></title>
		<link>https://community.jforum.net/posts/list/5.page</link>
		<description><![CDATA[Latest messages posted in the topic "Making post from another tomcat application"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>Making post from another tomcat application</title>
				<description><![CDATA[ Hi,
<br>
<br>
Does anyone know how I can make a post from another webapp application? We have an existing web application that when a user submit a form, we would like it to make a new topic on the jforum. I have tried following the saveMessage method in InstallAction class and have successfully added the post into the database, but somehow I don't see the new post in the jforum.
<br>
<br>
Any help or suggestion would be appreciated.
<br>
<br>
Thanks.]]></description>
				<guid isPermaLink="true">https://community.jforum.net/posts/preList/17/50.page</guid>
				<link>https://community.jforum.net/posts/preList/17/50.page</link>
				<pubDate><![CDATA[Fri, 30 Sep 2011 05:30:37]]> GMT</pubDate>
				<author><![CDATA[ jforumrookie]]></author>
			</item>
			<item>
				<title>回覆:Making post from another tomcat application</title>
				<description><![CDATA[ You can use [url=http://hc.apache.org/index.html]Apache HttpComponents[/url] HttpClient to do this. However, JForum needs to have a new API postApi to be called by your application.
<br>
<br>
The scenario is as follows:
<br>
[Your AP] ---HTTP POST---&gt;[JForum postApi]
<br>
<br>
Your AP makes an HTTP POST to the JForum postApi URL with 5 parameters:
<br>
1. api_key: used as a trust key between Your AP and JForum
<br>
2. email: poster's email address (should be a user of JForum first)
<br>
3. forum_id: the target forum's id
<br>
4. subject: subject of the post
<br>
5. message: content of the post
<br>
<br>
For security, you need to add an api_key into the jforum_api table. (api_validity should be a longer future date)
<br>
[code]insert into jforum_api(api_key, api_validity) values('yourAp', '2012/12/22');[/code]
<br>
<br>
The postApi pattern looks like the following:
<br>
http://localhsot:8080/jforum/postApi/insert/yourAp/somebody@yourcompany.com/1.page
<br>
<br>
You need to add this pattern into jforum's /WEB-INF/config/urlPattern.properties
<br>
[code]
<br>
# postApi
<br>
postApi.insert.3 = api_key, email, forum_id[/code]
<br>
<br>
Finally, I have added a new API in JForum 2.3.2 to help this need. I add this into jforum's /WEB-INF/config/modulesMapping.properties
<br>
[code]postApi = net.jforum.api.rest.PostREST[/code]
<br>
<br>
The PostREST.java is as follows:
<br>
[code]
<br>
<br>
/*
<br>
* Copyright (c) JForum Team
<br>
* All rights reserved.
<br>
*
<br>
* Redistribution and use in source and binary forms,
<br>
* with or without modification, are permitted provided
<br>
* that the following conditions are met:
<br>
*
<br>
* 1) Redistributions of source code must retain the above
<br>
* copyright notice, this list of conditions and the
<br>
* following disclaimer.
<br>
* 2) Redistributions in binary form must reproduce the
<br>
* above copyright notice, this list of conditions and
<br>
* the following disclaimer in the documentation and/or
<br>
* other materials provided with the distribution.
<br>
* 3) Neither the name of "Rafael Steil" nor
<br>
* the names of its contributors may be used to endorse
<br>
* or promote products derived from this software without
<br>
* specific prior written permission.
<br>
*
<br>
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
<br>
* HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
<br>
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
<br>
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
<br>
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR
<br>
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
<br>
* THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
<br>
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
<br>
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES
<br>
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
<br>
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
<br>
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
<br>
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
<br>
* IN CONTRACT, STRICT LIABILITY, OR TORT
<br>
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
<br>
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
<br>
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
<br>
*
<br>
* Created on 01/10/2011 14:32:22
<br>
* The JForum Project
<br>
* http://www.jforum.net
<br>
*/
<br>
package net.jforum.api.rest;
<br>
<br>
import java.util.Date;
<br>
<br>
import net.jforum.Command;
<br>
import net.jforum.JForumExecutionContext;
<br>
import net.jforum.SessionFacade;
<br>
import net.jforum.context.RequestContext;
<br>
import net.jforum.context.ResponseContext;
<br>
import net.jforum.dao.DataAccessDriver;
<br>
import net.jforum.dao.UserDAO;
<br>
import net.jforum.entities.Post;
<br>
import net.jforum.entities.Topic;
<br>
import net.jforum.entities.User;
<br>
import net.jforum.entities.UserSession;
<br>
import net.jforum.exceptions.APIException;
<br>
import net.jforum.util.I18n;
<br>
import net.jforum.util.preferences.ConfigKeys;
<br>
import net.jforum.util.preferences.SystemGlobals;
<br>
import net.jforum.util.preferences.TemplateKeys;
<br>
import net.jforum.view.forum.PostAction;
<br>
<br>
import org.apache.commons.lang3.StringUtils;
<br>
<br>
import freemarker.template.SimpleHash;
<br>
import freemarker.template.Template;
<br>
<br>
/**
<br>
* @author Andowson Chang
<br>
*
<br>
*/
<br>
public class PostREST extends Command {
<br>
<br>
/* (non-Javadoc)
<br>
* @see net.jforum.Command#list()
<br>
*/
<br>
@Override
<br>
public void list() {
<br>
try {
<br>
this.authenticate();
<br>
// do nothing here
<br>
// TODO: add implementation
<br>
}
<br>
catch (Exception e) {
<br>
this.setTemplateName(TemplateKeys.API_ERROR);
<br>
this.context.put("exception", e);
<br>
}
<br>
}
<br>
<br>
/**
<br>
* Creates a new post.
<br>
* Required parameters are "email", "forum_id", "subject" and "message".
<br>
*/
<br>
public void insert()
<br>
{
<br>
try {
<br>
this.authenticate();
<br>
<br>
final String email = this.requiredRequestParameter("email");
<br>
final String forumId = this.requiredRequestParameter("forum_id");
<br>
final String subject = this.requiredRequestParameter("subject");
<br>
final String message = this.requiredRequestParameter("message");
<br>
<br>
final UserDAO dao = DataAccessDriver.getInstance().newUserDAO();
<br>
User user = dao.findByEmail(email);
<br>
// If user's email not exists, use anonymous instead
<br>
if (user == null) {
<br>
user = new User();
<br>
user.setId(SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID));
<br>
user.setUsername(I18n.getMessage("Guest"));
<br>
}
<br>
// OK, time to insert the post
<br>
final UserSession userSession = SessionFacade.getUserSession();
<br>
userSession.setUserId(user.getId());
<br>
userSession.setUsername(user.getUsername());
<br>
String sessionId = userSession.getSessionId();
<br>
userSession.setStartTime(new Date(System.currentTimeMillis()));
<br>
SessionFacade.makeLogged();
<br>
<br>
SessionFacade.removeAttribute(ConfigKeys.LAST_POST_TIME);
<br>
SessionFacade.setAttribute(ConfigKeys.REQUEST_IGNORE_CAPTCHA, "1");
<br>
<br>
final Post post = new Post();
<br>
post.setForumId(Integer.valueOf(forumId));
<br>
post.setSubject(subject);
<br>
post.setText(message);
<br>
this.insertMessage(user, post);
<br>
SessionFacade.makeUnlogged();
<br>
SessionFacade.remove(sessionId);
<br>
}
<br>
catch (Exception e) {
<br>
this.setTemplateName(TemplateKeys.API_ERROR);
<br>
this.context.put("exception", e);
<br>
}
<br>
}
<br>
<br>
/**
<br>
* Calls {@link PostAction#insertSave()}
<br>
* @param post the post
<br>
* @param user the user who's doing the post
<br>
*/
<br>
private void insertMessage(final User user, final Post post)
<br>
{
<br>
this.addDataToRequest(user, post);
<br>
<br>
final PostAction postAction = new PostAction(JForumExecutionContext.getRequest(), new SimpleHash());
<br>
postAction.insertSave();
<br>
}
<br>
<br>
/**
<br>
* Extracts information from a mail message and adds it to the request context
<br>
* @param post the post
<br>
* @param user the user who's doing the post
<br>
*/
<br>
private void addDataToRequest(final User user, final Post post)
<br>
{
<br>
final RequestContext request = JForumExecutionContext.getRequest();
<br>
<br>
request.addParameter("topic_type", Integer.toString(Topic.TYPE_NORMAL));
<br>
request.addParameter("quick", "1");
<br>
<br>
final int topicId = post.getTopicId();
<br>
if (topicId &gt; 0) {
<br>
request.addParameter("topic_id", Integer.toString(topicId));
<br>
}
<br>
<br>
if (!user.isBbCodeEnabled()) {
<br>
request.addParameter("disable_bbcode", "on");
<br>
}
<br>
<br>
if (!user.isSmiliesEnabled()) {
<br>
request.addParameter("disable_smilies", "on");
<br>
}
<br>
<br>
if (!user.isHtmlEnabled()) {
<br>
request.addParameter("disable_html", "on");
<br>
}
<br>
}
<br>
<br>
/**
<br>
* Retrieves a parameter from the request and ensures it exists
<br>
* @param paramName the parameter name to retrieve its value
<br>
* @return the parameter value
<br>
* @throws APIException if the parameter is not found or its value is empty
<br>
*/
<br>
private String requiredRequestParameter(final String paramName)
<br>
{
<br>
final String value = this.request.getParameter(paramName);
<br>
<br>
if (StringUtils.isBlank(value)) {
<br>
throw new APIException("The parameter '" + paramName + "' was not found");
<br>
}
<br>
<br>
return value;
<br>
}
<br>
<br>
/**
<br>
* Tries to authenticate the user accessing the API
<br>
* @throws APIException if the authentication fails
<br>
*/
<br>
private void authenticate()
<br>
{
<br>
final String apiKey = this.requiredRequestParameter("api_key");
<br>
<br>
final RESTAuthentication auth = new RESTAuthentication();
<br>
<br>
if (!auth.validateApiKey(apiKey)) {
<br>
throw new APIException("The provided API authentication information is not valid");
<br>
}
<br>
}
<br>
<br>
public Template process(final RequestContext request, final ResponseContext response, final SimpleHash context)
<br>
{
<br>
JForumExecutionContext.setContentType("text/xml");
<br>
return super.process(request, response, context);
<br>
}
<br>
}
<br>
[/code]
<br>
<br>
And you can use the following example to do the test:
<br>
[code]
<br>
<br>
package com.andowson.integration;
<br>
<br>
import java.util.ArrayList;
<br>
import java.util.List;
<br>
<br>
import org.apache.http.HttpEntity;
<br>
import org.apache.http.HttpResponse;
<br>
import org.apache.http.NameValuePair;
<br>
import org.apache.http.client.entity.UrlEncodedFormEntity;
<br>
import org.apache.http.client.methods.HttpPost;
<br>
import org.apache.http.impl.client.DefaultHttpClient;
<br>
import org.apache.http.message.BasicNameValuePair;
<br>
import org.apache.http.protocol.HTTP;
<br>
import org.apache.http.util.EntityUtils;
<br>
<br>
/**
<br>
* A example that demonstrates how HttpClient APIs can be used to perform
<br>
* adding a post outside JForum.
<br>
*/
<br>
public class AddPostOutsideJForum {
<br>
<br>
public static void main(String[] args) throws Exception {
<br>
<br>
DefaultHttpClient httpclient = new DefaultHttpClient();
<br>
try {
<br>
String apiKey = "yourAp";
<br>
String email = "somebody@yourcompany.com";
<br>
int forumId = 1;
<br>
String subject = "postApi test";
<br>
String message = "This is a test post outside JForum.\nBBCode test :)";
<br>
String baseURL = "http://localhost:8080/jforum/";
<br>
<br>
HttpPost httppost = new HttpPost(baseURL + "postApi/insert/" +
<br>
apiKey + "/" + email + "/" + forumId + ".page");
<br>
<br>
List &lt;NameValuePair&gt; nvps = new ArrayList &lt;NameValuePair&gt;();
<br>
nvps.add(new BasicNameValuePair("subject", subject));
<br>
nvps.add(new BasicNameValuePair("message", message));
<br>
<br>
httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
<br>
<br>
System.out.println("executing request " + httppost.getURI());
<br>
<br>
HttpResponse response = httpclient.execute(httppost);
<br>
HttpEntity entity = response.getEntity();
<br>
System.out.println("status: " + response.getStatusLine());
<br>
System.out.println(EntityUtils.toString(entity));
<br>
} finally {
<br>
// When HttpClient instance is no longer needed,
<br>
// shut down the connection manager to ensure
<br>
// immediate deallocation of all system resources
<br>
httpclient.getConnectionManager().shutdown();
<br>
}
<br>
}
<br>
}
<br>
[/code]]]></description>
				<guid isPermaLink="true">https://community.jforum.net/posts/preList/17/51.page</guid>
				<link>https://community.jforum.net/posts/preList/17/51.page</link>
				<pubDate><![CDATA[Sat, 1 Oct 2011 23:08:39]]> GMT</pubDate>
				<author><![CDATA[ andowson]]></author>
			</item>
			<item>
				<title>Re:Making post from another tomcat application</title>
				<description><![CDATA[ Thanks! I will give this a try.]]></description>
				<guid isPermaLink="true">https://community.jforum.net/posts/preList/17/52.page</guid>
				<link>https://community.jforum.net/posts/preList/17/52.page</link>
				<pubDate><![CDATA[Tue, 4 Oct 2011 02:39:46]]> GMT</pubDate>
				<author><![CDATA[ jforumrookie]]></author>
			</item>
			<item>
				<title>Re:Making post from another tomcat application</title>
				<description><![CDATA[ Thanks andowson for this added feature. It works beautifully. One more request if possible. Is it possible for the PostREST to return the post id or a link to the post after the insert?
<br>
<br>
I tried to mod it like UserREST, but no success. I keep getting a response of 302 Move Temporary.
<br>
<br>
Thanks,]]></description>
				<guid isPermaLink="true">https://community.jforum.net/posts/preList/17/55.page</guid>
				<link>https://community.jforum.net/posts/preList/17/55.page</link>
				<pubDate><![CDATA[Sat, 29 Oct 2011 06:12:14]]> GMT</pubDate>
				<author><![CDATA[ jforumrookie]]></author>
			</item>
			<item>
				<title>回覆:Making post from another tomcat application</title>
				<description><![CDATA[ I've tried to do the same thing as you said during the implementation. And keep getting a response of 302 Move Temporary, too.
<br>
So finally I decide to make it works as only support adding a post outside but no post id returned.
<br>
<br>
You may tried to modify the PostAction.java's insertSave() method's return type from void to int and get the postId returned.]]></description>
				<guid isPermaLink="true">https://community.jforum.net/posts/preList/17/56.page</guid>
				<link>https://community.jforum.net/posts/preList/17/56.page</link>
				<pubDate><![CDATA[Sat, 29 Oct 2011 12:52:40]]> GMT</pubDate>
				<author><![CDATA[ andowson]]></author>
			</item>
			<item>
				<title>Re:Making post from another tomcat application</title>
				<description><![CDATA[ Thanks. I'll give it a try.]]></description>
				<guid isPermaLink="true">https://community.jforum.net/posts/preList/17/57.page</guid>
				<link>https://community.jforum.net/posts/preList/17/57.page</link>
				<pubDate><![CDATA[Mon, 31 Oct 2011 23:08:44]]> GMT</pubDate>
				<author><![CDATA[ jforumrookie]]></author>
			</item>
			<item>
				<title>Re:Making post from another tomcat application</title>
				<description><![CDATA[ Thanks andowson. After some debugging, it looks like PostAction.insertSave is redirecting the path. So I didn't change any of its code. What I ended up doing was in the PostREST, I retrieve the post link from JForumExecutionContext.getRedirectTo() and then set it to null with JForumExecutionContext.setRedirect(null). That seem to do the trick.
<br>
<br>
Here's a sample snipplet of my modified PostREST.java
<br>
[code]
<br>
public void insert()
<br>
{
<br>
...
<br>
final Post post = new Post();
<br>
post.setForumId(Integer.valueOf(forumId));
<br>
post.setSubject(subject);
<br>
post.setText(message);
<br>
this.insertMessage(user, post);
<br>
<br>
// new start
<br>
String postLink = JForumExecutionContext.getRedirectTo();
<br>
JForumExecutionContext.setRedirect(null);
<br>
this.setTemplateName(TemplateKeys.API_POST_INSERT);
<br>
this.context.put("postLink", postLink);
<br>
// new end
<br>
<br>
SessionFacade.makeUnlogged();
<br>
SessionFacade.remove(sessionId);
<br>
...
<br>
}
<br>
[/code]
<br>
Thanks!]]></description>
				<guid isPermaLink="true">https://community.jforum.net/posts/preList/17/58.page</guid>
				<link>https://community.jforum.net/posts/preList/17/58.page</link>
				<pubDate><![CDATA[Tue, 1 Nov 2011 02:06:05]]> GMT</pubDate>
				<author><![CDATA[ jforumrookie]]></author>
			</item>
			<item>
				<title>回覆:Making post from another tomcat application</title>
				<description><![CDATA[ Nice! I'll add this to the next release.
<br>
Thank you, jforumrookie.]]></description>
				<guid isPermaLink="true">https://community.jforum.net/posts/preList/17/59.page</guid>
				<link>https://community.jforum.net/posts/preList/17/59.page</link>
				<pubDate><![CDATA[Tue, 1 Nov 2011 08:09:15]]> GMT</pubDate>
				<author><![CDATA[ andowson]]></author>
			</item>
	</channel>
</rss>