protected void remoteServeResource( ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws Exception { ThemeDisplay themeDisplay = (ThemeDisplay) resourceRequest.getAttribute(WebKeys.THEME_DISPLAY); OAuthRequest oAuthRequest = new OAuthRequest(Verb.GET, getServerPortletURL()); setRequestParameters(resourceRequest, resourceResponse, oAuthRequest); addOAuthParameter(oAuthRequest, "p_p_lifecycle", "2"); addOAuthParameter(oAuthRequest, "p_p_resource_id", resourceRequest.getResourceID()); Response response = getResponse(themeDisplay.getUser(), oAuthRequest); String contentType = response.getHeader(HttpHeaders.CONTENT_TYPE); if (contentType.startsWith(ContentTypes.APPLICATION_OCTET_STREAM)) { String contentDisposition = response.getHeader(HttpHeaders.CONTENT_DISPOSITION); int contentLength = GetterUtil.getInteger(response.getHeader(HttpHeaders.CONTENT_LENGTH)); PortletResponseUtil.sendFile( resourceRequest, resourceResponse, getFileName(contentDisposition), response.getStream(), contentLength, contentType, HttpHeaders.CONTENT_DISPOSITION_ATTACHMENT); } else { PortletResponseUtil.write(resourceResponse, response.getStream()); } }
protected void remoteProcessAction(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception { ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY); OAuthRequest oAuthRequest = new OAuthRequest(Verb.POST, getServerPortletURL()); setRequestParameters(actionRequest, actionResponse, oAuthRequest); addOAuthParameter(oAuthRequest, "p_p_lifecycle", "1"); addOAuthParameter(oAuthRequest, "p_p_state", WindowState.NORMAL.toString()); Response response = getResponse(themeDisplay.getUser(), oAuthRequest); if (response.getCode() == HttpServletResponse.SC_FOUND) { String redirectLocation = response.getHeader(HttpHeaders.LOCATION); actionResponse.sendRedirect(redirectLocation); } else { HttpServletResponse httpServletResponse = PortalUtil.getHttpServletResponse(actionResponse); httpServletResponse.setContentType(response.getHeader(HttpHeaders.CONTENT_TYPE)); ServletResponseUtil.write(httpServletResponse, response.getStream()); } }
public static void saveAccessToken(HttpServletRequest request, OAuthService service, Token accessToken) throws Exception { OAuthRequest _request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL); service.signRequest(accessToken, _request); Response _response = _request.send(); if (_response.getCode() != 200) throw new OAuthException("Can query account information."); String contentType = _response.getHeader("Content-Type"); if (contentType == null) contentType = ""; //String charset = ""; int semicolonPos = contentType.indexOf(';'); if (semicolonPos > 0) { String _charset = contentType.substring(semicolonPos + 1).trim(); if (_charset.startsWith("charset")) { //charset = _charset.substring(_charset.indexOf('=') + 1); } contentType = contentType.substring(0, semicolonPos); } Map<String, String> responseAttributes = null; String response = _response.getBody(); if ("application/json".equals(contentType) || (response.startsWith("{") && response.endsWith("}"))) { JSONObject jsonResponse = new JSONObject(response); if (jsonResponse != null) { if (jsonResponse.has("error")) { throw new OAuthException("Error getting access token: " + System.getProperty("line.separator") + jsonResponse.toString()); } responseAttributes = parseJSONObject(jsonResponse); } } else if ("text/plain".equals(contentType) || (response.contains("=") && response.contains("&"))) { //responseAttributes = OAuthUtil.parseQueryString(response); } if (responseAttributes == null) throw new OAuthException("Get response, but no account information."); String id = responseAttributes.get("id"); String accountName = id + "@facebook.com"; Account found = OAuthRealm._.getAccount(accountName); if (found == null) { Map<SchemaType, String> metadata = new HashMap<SchemaType, String>(); metadata.put(GoogleSchemaType.ID, responseAttributes.get("id")); metadata.put(AXSchemaType.FIRSTNAME, responseAttributes.get("given_name")); metadata.put(AXSchemaType.LASTNAME, responseAttributes.get("family_name")); metadata.put(AXSchemaType.FULLNAME, responseAttributes.get("name")); metadata.put(AXSchemaType.TIMEZONE, responseAttributes.get("timezone")); found = OAuthRealm._.createAccountInDatabase(accountName, metadata); } Account principal = new SubjectAccreditedImpl((AbstractAccount) found, accessToken); HttpSession session = request.getSession(true); Subject subject = new Subject(); //TODO: hardcoded to jetty - rewrite //******************************************************* DefaultIdentityService _identityService = new DefaultIdentityService(); UserIdentity user = _identityService.newUserIdentity(subject, principal, new String[0]); Authentication cached=new HttpSessionAuthentication(session, user); session.setAttribute(HttpSessionAuthentication.__J_AUTHENTICATED, cached); //******************************************************* request.getSession().setAttribute(GOOGLE_ACCESS_TOKEN_SESSION, accessToken); }