001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.servlet;
016    
017    import com.liferay.portal.atom.AtomProvider;
018    import com.liferay.portal.atom.AtomUtil;
019    import com.liferay.portal.kernel.atom.AtomCollectionAdapter;
020    import com.liferay.portal.kernel.atom.AtomCollectionAdapterRegistryUtil;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.model.User;
024    import com.liferay.portal.security.auth.CompanyThreadLocal;
025    import com.liferay.portal.security.permission.PermissionChecker;
026    import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
027    import com.liferay.portal.security.permission.PermissionThreadLocal;
028    
029    import java.util.List;
030    
031    import javax.servlet.ServletException;
032    import javax.servlet.http.HttpServletRequest;
033    import javax.servlet.http.HttpServletResponse;
034    
035    import org.apache.abdera.protocol.server.Provider;
036    import org.apache.abdera.protocol.server.servlet.AbderaServlet;
037    
038    /**
039     * @author Igor Spasic
040     */
041    public class AtomServlet extends AbderaServlet {
042    
043            @Override
044            protected Provider createProvider() {
045                    AtomProvider atomProvider = new AtomProvider();
046    
047                    atomProvider.init(getAbdera(), null);
048    
049                    List<AtomCollectionAdapter<?>> atomCollectionAdapters =
050                            AtomCollectionAdapterRegistryUtil.getAtomCollectionAdapters();
051    
052                    for (AtomCollectionAdapter<?> atomCollectionAdapter :
053                                    atomCollectionAdapters) {
054    
055                            atomProvider.addCollection(atomCollectionAdapter);
056                    }
057    
058                    return atomProvider;
059            }
060    
061            @Override
062            protected void service(
063                            HttpServletRequest request, HttpServletResponse response)
064                    throws ServletException {
065    
066                    try {
067                            UserResolver userResolver = new UserResolver(request);
068    
069                            CompanyThreadLocal.setCompanyId(userResolver.getCompanyId());
070    
071                            User user = userResolver.getUser();
072    
073                            if (user != null) {
074                                    if (_log.isDebugEnabled()) {
075                                            _log.debug("User " + user.getUserId());
076                                    }
077    
078                                    PermissionChecker permissionChecker =
079                                            PermissionCheckerFactoryUtil.create(user);
080    
081                                    PermissionThreadLocal.setPermissionChecker(permissionChecker);
082    
083                                    AtomUtil.saveUserInRequest(request, user);
084                            }
085    
086                            super.service(request, response);
087                    }
088                    catch (Exception e) {
089                            throw new ServletException(e);
090                    }
091            }
092    
093            private static Log _log = LogFactoryUtil.getLog(AtomServlet.class);
094    
095    }