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.model.User;
022    import com.liferay.portal.security.ac.AccessControlThreadLocal;
023    import com.liferay.portal.security.ac.AccessControlUtil;
024    import com.liferay.portal.security.auth.AccessControlContext;
025    import com.liferay.portal.security.auth.AuthVerifierResult;
026    import com.liferay.portal.service.UserLocalServiceUtil;
027    
028    import java.util.List;
029    
030    import javax.servlet.ServletException;
031    import javax.servlet.http.HttpServletRequest;
032    import javax.servlet.http.HttpServletResponse;
033    
034    import org.apache.abdera.protocol.server.Provider;
035    import org.apache.abdera.protocol.server.servlet.AbderaServlet;
036    
037    /**
038     * @author Igor Spasic
039     */
040    public class AtomServlet extends AbderaServlet {
041    
042            @Override
043            protected Provider createProvider() {
044                    AtomProvider atomProvider = new AtomProvider();
045    
046                    atomProvider.init(getAbdera(), null);
047    
048                    List<AtomCollectionAdapter<?>> atomCollectionAdapters =
049                            AtomCollectionAdapterRegistryUtil.getAtomCollectionAdapters();
050    
051                    for (AtomCollectionAdapter<?> atomCollectionAdapter :
052                                    atomCollectionAdapters) {
053    
054                            atomProvider.addCollection(atomCollectionAdapter);
055                    }
056    
057                    return atomProvider;
058            }
059    
060            @Override
061            protected void service(
062                            HttpServletRequest request, HttpServletResponse response)
063                    throws ServletException {
064    
065                    boolean remoteAccess = AccessControlThreadLocal.isRemoteAccess();
066    
067                    try {
068                            AccessControlContext accessControlContext =
069                                    AccessControlUtil.getAccessControlContext();
070    
071                            AuthVerifierResult authVerifierResult =
072                                    accessControlContext.getAuthVerifierResult();
073    
074                            User user = UserLocalServiceUtil.getUser(
075                                    authVerifierResult.getUserId());
076    
077                            AtomUtil.saveUserInRequest(request, user);
078    
079                            AccessControlThreadLocal.setRemoteAccess(true);
080    
081                            super.service(request, response);
082                    }
083                    catch (Exception e) {
084                            throw new ServletException(e);
085                    }
086                    finally {
087                            AccessControlThreadLocal.setRemoteAccess(remoteAccess);
088                    }
089            }
090    
091    }