001package org.syncany.plugins.transfer.oauth; 002 003import java.io.InputStream; 004 005import org.apache.commons.io.IOUtils; 006import io.undertow.util.StatusCodes; 007 008/** 009 * Factory class to generate some common {@link OAuthWebResponse}s. It uses html sites residing in the ressource folder, 010 * but provides plain text fallbacks if a specific site cannot be read. 011 * 012 * @author Christian Roth (christian.roth@port17.de) 013 */ 014 015public abstract class OAuthWebResponses { 016 017 public static final String RESOURCE_DIR = "/org/syncany/plugins/oauth/"; 018 019 /** 020 * This {@link OAuthWebResponse} is used when a token was succesfully extracted. It uses <code>ValidWebResponse.html</code> which should be placed in 021 * {@value #RESOURCE_DIR}. 022 * 023 * @return Either the parsed html file or a fallback string. 024 */ 025 public static OAuthWebResponse createValidResponse() { 026 return new OAuthWebResponse(StatusCodes.OK, loadHtml("ValidWebResponse.html", "Token successfully extracted.")); 027 } 028 029 /** 030 * This {@link OAuthWebResponse} is used when there was an error during the OAuth process. It uses <code>BadRequestWebResponse.html</code> which 031 * should be placed in {@value #RESOURCE_DIR}. 032 * 033 * @return Either the parsed html file or a fallback string. 034 */ 035 public static OAuthWebResponse createBadResponse() { 036 return new OAuthWebResponse(StatusCodes.BAD_REQUEST, loadHtml("BadRequestWebResponse.html", "Error while acquiring token.")); 037 } 038 039 private static String loadHtml(String fileName, String fallbackString) { 040 String html = fallbackString; 041 042 try (InputStream resource = OAuthWebResponses.class.getResourceAsStream(RESOURCE_DIR + fileName)) { 043 html = IOUtils.toString(resource); 044 } 045 catch (Exception e) { 046 // use fallback plain string 047 } 048 049 return html; 050 } 051 052}