com.fujieid.jap.ids.endpoint.ErrorEndpoint Maven / Gradle / Ivy
Show all versions of jap-ids Show documentation
/*
* Copyright (c) 2020-2040, 北京符节科技有限公司 ([email protected] & https://www.fujieid.com).
*
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.gnu.org/licenses/lgpl.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.fujieid.jap.ids.endpoint;
import com.fujieid.jap.http.JapHttpRequest;
import com.fujieid.jap.http.JapHttpResponse;
import com.fujieid.jap.http.RequestUtil;
import com.fujieid.jap.ids.model.enums.ErrorResponse;
import com.xkcoding.json.util.StringUtil;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
/**
* OAuth 异常
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0.0
* @since 1.0.0
*/
public class ErrorEndpoint extends AbstractEndpoint {
/**
* Generate the html of the error authorization page
*
* @param error error type
* @param errorDescription error description
* @return error page html
*/
public String createErrorPageHtml(String error, String errorDescription) {
return generateErrorPageHtml(error, errorDescription);
}
/**
* Obtain exception information from the request url and display the exception page
*
* @param request current HTTP request
* @param response current HTTP response
* @throws IOException IOException
*/
public void showErrorPage(JapHttpRequest request, JapHttpResponse response) throws IOException {
ErrorResponse errorResponse = ErrorResponse.getByError(RequestUtil.getParam("error", request));
String errorPageHtml = createErrorPageHtml(errorResponse.getError(), errorResponse.getErrorDescription());
response.setContentType("text/html;charset=UTF-8");
response.setContentLength(errorPageHtml.getBytes(StandardCharsets.UTF_8).length);
response.write(errorPageHtml);
}
/**
* Display customized exception content
*
* @param error error type
* @param errorDescription error description
* @param response current HTTP response
* @throws IOException IOException
*/
public void showErrorPage(String error, String errorDescription, JapHttpResponse response) throws IOException {
String errorPageHtml = createErrorPageHtml(error, errorDescription);
response.setContentType("text/html;charset=UTF-8");
response.setContentLength(errorPageHtml.getBytes(StandardCharsets.UTF_8).length);
response.write(errorPageHtml);
}
private String generateErrorPageHtml(String error, String errorDescription) {
String html = "\n"
+ "\n"
+ "
\n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " Oops!, something went wrong \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " Oops!, something went wrong
\n";
if (StringUtil.isNotEmpty(error)) {
html += "" + error + "
";
}
html += " \n" + errorDescription + "
\n"
+ " Feel free to contact us.
\n"
+ " Please try again.
\n" +
"\n" +
"";
return html;
}
}