Search in sources :

Example 11 with ForumException

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);
    }
}
Also used : ForumException(net.jforum.exceptions.ForumException) SessionContext(net.jforum.context.SessionContext) SSOUtils(net.jforum.sso.SSOUtils) SSO(net.jforum.sso.SSO) DatabaseException(net.jforum.exceptions.DatabaseException) ForumException(net.jforum.exceptions.ForumException)

Example 12 with ForumException

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);
}
Also used : ForumException(net.jforum.exceptions.ForumException) IOException(java.io.IOException)

Example 13 with ForumException

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, "<", "&lt;");
                    ViewCommon.replaceAll(tmp, ">", "&gt;");
                    ViewCommon.replaceAll(tmp, "\"", "&quot;");
                    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, "<", "&lt;");
                ViewCommon.replaceAll(tmp, ">", "&gt;");
                sb.append(tmp.toString());
            }
        }
    } catch (Exception e) {
        throw new ForumException("Error while parsing HTML: " + e, e);
    }
    return sb.toString();
}
Also used : Lexer(org.htmlparser.lexer.Lexer) ForumException(net.jforum.exceptions.ForumException) Node(org.htmlparser.Node) TextNode(org.htmlparser.nodes.TextNode) TextNode(org.htmlparser.nodes.TextNode) Tag(org.htmlparser.Tag) ForumException(net.jforum.exceptions.ForumException)

Example 14 with ForumException

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);
    }
}
Also used : ForumException(net.jforum.exceptions.ForumException) Iterator(java.util.Iterator) ImageWriter(javax.imageio.ImageWriter) JPEGImageWriteParam(javax.imageio.plugins.jpeg.JPEGImageWriteParam) IOException(java.io.IOException) ImageWriteParam(javax.imageio.ImageWriteParam) JPEGImageWriteParam(javax.imageio.plugins.jpeg.JPEGImageWriteParam) File(java.io.File) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IIOImage(javax.imageio.IIOImage)

Example 15 with ForumException

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);
    }
}
Also used : ForumException(net.jforum.exceptions.ForumException) Document(org.apache.lucene.document.Document) IOException(java.io.IOException) ForumException(net.jforum.exceptions.ForumException)

Aggregations

ForumException (net.jforum.exceptions.ForumException)37 IOException (java.io.IOException)24 FileInputStream (java.io.FileInputStream)10 File (java.io.File)7 Iterator (java.util.Iterator)6 Properties (java.util.Properties)6 DatabaseException (net.jforum.exceptions.DatabaseException)6 List (java.util.List)5 SQLException (java.sql.SQLException)4 CacheEngineStartupException (net.jforum.exceptions.CacheEngineStartupException)4 SchedulerException (org.quartz.SchedulerException)4 FileOutputStream (java.io.FileOutputStream)3 PreparedStatement (java.sql.PreparedStatement)3 Enumeration (java.util.Enumeration)3 Statement (java.sql.Statement)2 ArrayList (java.util.ArrayList)2 SAXParser (javax.xml.parsers.SAXParser)2 Post (net.jforum.entities.Post)2 User (net.jforum.entities.User)2 SSO (net.jforum.sso.SSO)2