use of net.jforum.exceptions.ForumException in project jforum2 by rafaelsteil.
the class ControllerUtils method checkSSO.
/**
* Checks for user authentication using some SSO implementation
* @param userSession UserSession
*/
protected void checkSSO(UserSession userSession) {
try {
SSO sso = (SSO) Class.forName(SystemGlobals.getValue(ConfigKeys.SSO_IMPLEMENTATION)).newInstance();
String username = sso.authenticateUser(JForumExecutionContext.getRequest());
if (username == null || username.trim().equals("")) {
userSession.makeAnonymous();
} else {
SSOUtils utils = new SSOUtils();
if (!utils.userExists(username)) {
SessionContext session = JForumExecutionContext.getRequest().getSessionContext();
String email = (String) session.getAttribute(SystemGlobals.getValue(ConfigKeys.SSO_EMAIL_ATTRIBUTE));
String password = (String) session.getAttribute(SystemGlobals.getValue(ConfigKeys.SSO_PASSWORD_ATTRIBUTE));
if (email == null) {
email = SystemGlobals.getValue(ConfigKeys.SSO_DEFAULT_EMAIL);
}
if (password == null) {
password = SystemGlobals.getValue(ConfigKeys.SSO_DEFAULT_PASSWORD);
}
utils.register(password, email);
}
this.configureUserSession(userSession, utils.getUser());
}
} catch (Exception e) {
e.printStackTrace();
throw new ForumException("Error while executing SSO actions: " + e);
}
}
use of net.jforum.exceptions.ForumException in project jforum2 by rafaelsteil.
the class JForumExecutionContext method requestBasicAuthentication.
/**
* Send UNAUTHORIZED to the browser and ask user to login via basic authentication
*/
public static void requestBasicAuthentication() {
getResponse().addHeader("WWW-Authenticate", "Basic realm=\"JForum\"");
try {
getResponse().sendError(HttpServletResponse.SC_UNAUTHORIZED);
} catch (IOException e) {
throw new ForumException(e);
}
enableCustomContent(true);
}
use of net.jforum.exceptions.ForumException in project jforum2 by rafaelsteil.
the class SafeHtml method makeSafe.
/**
* Given an input, makes it safe for HTML displaying.
* Removes any not allowed HTML tag or attribute, as well
* unwanted Javascript statements inside the tags.
* @param contents the input to analyze
* @return the modified and safe string
*/
public String makeSafe(String contents) {
if (contents == null || contents.length() == 0) {
return contents;
}
StringBuffer sb = new StringBuffer(contents.length());
try {
Lexer lexer = new Lexer(contents);
Node node;
while ((node = lexer.nextNode()) != null) {
boolean isTextNode = node instanceof TextNode;
if (isTextNode) {
// Text nodes are raw data, so we just
// strip off all possible html content
String text = node.toHtml();
if (text.indexOf('>') > -1 || text.indexOf('<') > -1) {
StringBuffer tmp = new StringBuffer(text);
ViewCommon.replaceAll(tmp, "<", "<");
ViewCommon.replaceAll(tmp, ">", ">");
ViewCommon.replaceAll(tmp, "\"", """);
node.setText(tmp.toString());
}
}
if (isTextNode || (node instanceof Tag && this.isTagWelcome(node))) {
sb.append(node.toHtml());
} else {
StringBuffer tmp = new StringBuffer(node.toHtml());
ViewCommon.replaceAll(tmp, "<", "<");
ViewCommon.replaceAll(tmp, ">", ">");
sb.append(tmp.toString());
}
}
} catch (Exception e) {
throw new ForumException("Error while parsing HTML: " + e, e);
}
return sb.toString();
}
use of net.jforum.exceptions.ForumException in project jforum2 by rafaelsteil.
the class ImageUtils method saveCompressedImage.
/**
* Compress and save an image to the disk. Currently this method only supports JPEG images.
*
* @param image The image to save
* @param toFileName The filename to use
* @param type The image type. Use <code>ImageUtils.IMAGE_JPEG</code> to save as JPEG images,
* or <code>ImageUtils.IMAGE_PNG</code> to save as PNG.
*/
public static void saveCompressedImage(BufferedImage image, String toFileName, int type) {
try {
if (type == IMAGE_PNG) {
throw new UnsupportedOperationException("PNG compression not implemented");
}
Iterator iter = ImageIO.getImageWritersByFormatName("jpg");
ImageWriter writer;
writer = (ImageWriter) iter.next();
ImageOutputStream ios = ImageIO.createImageOutputStream(new File(toFileName));
writer.setOutput(ios);
ImageWriteParam iwparam = new JPEGImageWriteParam(Locale.getDefault());
iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwparam.setCompressionQuality(0.7F);
writer.write(null, new IIOImage(image, null, null), iwparam);
ios.flush();
writer.dispose();
ios.close();
} catch (IOException e) {
throw new ForumException(e);
}
}
use of net.jforum.exceptions.ForumException in project jforum2 by rafaelsteil.
the class LuceneContentCollector method collect.
/**
* @see net.jforum.search.LuceneResultCollector#collect(SearchArgs, org.apache.lucene.search.Hits, org.apache.lucene.search.Query)
*/
public List collect(SearchArgs args, Hits hits, Query query) {
try {
int[] postIds = new int[Math.min(args.fetchCount(), hits.length())];
for (int docIndex = args.startFrom(), i = 0; docIndex < args.startFrom() + args.fetchCount() && docIndex < hits.length(); docIndex++, i++) {
Document doc = hits.doc(docIndex);
postIds[i] = Integer.parseInt(doc.get(SearchFields.Keyword.POST_ID));
}
return this.retrieveRealPosts(postIds, query);
} catch (Exception e) {
throw new ForumException(e.toString(), e);
}
}
Aggregations