use of java.io.UnsupportedEncodingException in project OpenAM by OpenRock.
the class RedirectToRealmHomeViewBean method redirectToHome.
protected void redirectToHome() {
if (XuiRedirectHelper.isXuiAdminConsoleEnabled()) {
RequestContext rc = RequestManager.getRequestContext();
try {
String realm = URLEncoder.encode(rc.getRequest().getParameter("realm"), "UTF-8");
rc.getResponse().sendRedirect("../XUI#realms/" + realm + "/dashboard");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("UTF-8 not supported", e);
} catch (IOException e) {
debug.message("Unexpected IOException during redirect", e);
}
} else {
HomeViewBean vb = (HomeViewBean) getViewBean(HomeViewBean.class);
backTrail();
passPgSessionMap(vb);
vb.forwardTo(getRequestContext());
}
}
use of java.io.UnsupportedEncodingException in project OpenAM by OpenRock.
the class ResourceResultCache method postForm.
String postForm(SSOToken appToken, String url, String formContent) throws PolicyException {
if (debug.messageEnabled()) {
debug.message("ResourceResultCache." + "postForm():" + "url=" + url + ", formContent=" + formContent);
}
StringBuilder sb = new StringBuilder();
HttpURLConnection conn = null;
OutputStream out = null;
BufferedReader reader = null;
try {
conn = HttpURLConnectionManager.getConnection(new URL(url));
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
setCookieAndHeader(conn, appToken, appToken);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", Integer.toString(formContent.length()));
conn.connect();
out = conn.getOutputStream();
out.write(formContent.getBytes("UTF-8"));
out.write("\r\n".getBytes("UTF-8"));
out.flush();
out.close();
reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
int len;
char[] buf = new char[1024];
while ((len = reader.read(buf, 0, buf.length)) != -1) {
sb.append(buf, 0, len);
}
int responseCode = conn.getResponseCode();
// any 200 series response code is success
if (responseCode < 200 || responseCode > 299) {
if (debug.warningEnabled()) {
debug.warning("ResourceResultCache." + "postForm():" + "REST call failed with HTTP response code:" + responseCode);
}
throw new PolicyException("Entitlement REST call failed with error code:" + responseCode);
}
} catch (UnsupportedEncodingException uee) {
// should not happen
debug.error("ResourceResultCache.postFormParams():" + "UnsupportedEncodingException:" + uee.getMessage());
} catch (IOException ie) {
debug.error("ResourceResultCache.postForm():IOException:" + ie.getMessage(), ie);
throw new PolicyException(ResBundleUtils.rbName, "rest_call_failed_with_io_exception", null, ie);
} finally {
try {
if (reader != null) {
reader.close();
}
if (conn != null) {
conn.disconnect();
}
} catch (Exception e) {
// ignore
}
}
return sb.toString();
}
use of java.io.UnsupportedEncodingException in project OpenAM by OpenRock.
the class ResourceResultCache method getResourceContent.
String getResourceContent(SSOToken appToken, SSOToken userToken, String url) throws PolicyException {
StringBuilder sb = new StringBuilder();
HttpURLConnection conn = null;
BufferedReader reader = null;
try {
conn = HttpURLConnectionManager.getConnection(new URL(url));
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("GET");
conn.setInstanceFollowRedirects(false);
setCookieAndHeader(conn, appToken, userToken);
conn.connect();
reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
int len;
char[] buf = new char[1024];
while ((len = reader.read(buf, 0, buf.length)) != -1) {
sb.append(buf, 0, len);
}
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP) {
// got a 302
if (debug.warningEnabled()) {
debug.warning("ResourceResultCache.getResourceContent():" + "got 302 redirect");
debug.warning("ResourceResultCache.getResourceContent():" + "throwing InvalidAppSSOTokenException");
}
String[] args = { conn.getResponseMessage() };
throw new InvalidAppSSOTokenException(ResBundleUtils.rbName, "rest_call_to_server_caused_302", args, null);
} else if (responseCode != HttpURLConnection.HTTP_OK) {
if (debug.warningEnabled()) {
debug.warning("ResourceResultCache.getResourceContent():" + "REST call failed with HTTP response code:" + responseCode);
}
throw new PolicyException("Entitlement REST call failed with error code:" + responseCode);
}
} catch (UnsupportedEncodingException uee) {
// should not happen
debug.error("ResourceResultCache.getResourceContent():" + "UnsupportedEncodingException:" + uee.getMessage());
} catch (IOException ie) {
debug.error("IOException:" + ie);
throw new PolicyException(ResBundleUtils.rbName, "rest_call_failed_with_io_exception", null, ie);
} finally {
try {
if (reader != null) {
reader.close();
}
if (conn != null) {
conn.disconnect();
}
} catch (Exception e) {
// ignore
}
}
return sb.toString();
}
use of java.io.UnsupportedEncodingException in project OpenAM by OpenRock.
the class ResourceResultCache method buildEntitlementRequestQueryString.
static String buildEntitlementRequestQueryString(String realm, String serviceName, SSOToken userToken, String resource, Set actionNames, Map envMap) throws PolicyException {
StringBuilder sb = new StringBuilder();
try {
realm = (realm == null || (realm.trim().length() == 0)) ? "/" : realm;
realm = URLEncoder.encode(realm, "UTF-8");
sb.append(REST_QUERY_REALM).append("=");
sb.append(realm);
if ((serviceName == null) || (serviceName.length() == 0)) {
if (debug.warningEnabled()) {
debug.warning("ResourceResultCache." + "buildEntitlementRequestQueryString():" + "serviceName can not be null");
}
throw new PolicyException(ResBundleUtils.rbName, "service_name_can_not_be_null", null, null);
} else {
sb.append("&").append(REST_QUERY_APPLICATION).append("=");
sb.append(URLEncoder.encode(serviceName, "UTF-8"));
}
if (userToken == null) {
if (debug.warningEnabled()) {
debug.warning("ResourceResultCache." + "buildEntitlementRequestQueryString():" + "subject can not be null");
}
throw new PolicyException(ResBundleUtils.rbName, "subject_can_not_be_null", null, null);
} else {
String userTokenId = userToken.getTokenID().toString();
String hashedUserTokenId = Hash.hash(userTokenId);
sb.append("&").append(REST_QUERY_SUBJECT).append("=");
sb.append(URLEncoder.encode(hashedUserTokenId, "UTF-8"));
}
if ((resource == null) || (resource.trim().length() == 0)) {
if (debug.warningEnabled()) {
debug.warning("ResourceResultCache." + "buildEntitlementRequestQueryString():" + "resource can not be null");
}
throw new PolicyException(ResBundleUtils.rbName, "resource_can_not_be_null", null, null);
} else {
sb.append("&").append(REST_QUERY_RESOURCE).append("=");
sb.append(URLEncoder.encode(resource, "UTF-8"));
}
if ((actionNames != null) && !actionNames.isEmpty()) {
for (Object actObj : actionNames) {
sb.append("&").append(REST_QUERY_ACTION).append("=");
sb.append(URLEncoder.encode(actObj.toString(), "UTF-8"));
}
}
if ((envMap != null) && !envMap.isEmpty()) {
String encodedEq = URLEncoder.encode("=", "UTF-8");
Set keys = envMap.keySet();
for (Object keyOb : keys) {
Set values = (Set) envMap.get(keyOb);
String key = URLEncoder.encode(keyOb.toString(), "UTF-8");
if ((values != null) && !values.isEmpty()) {
for (Object valueOb : values) {
sb.append("&").append(REST_QUERY_ENV).append("=");
sb.append(key);
sb.append(encodedEq);
sb.append(URLEncoder.encode(valueOb.toString(), "UTF-8"));
}
}
}
}
} catch (UnsupportedEncodingException use) {
// should not happen
debug.error("ResourceResultCache.buildEntitlementRequestQueryString():" + use.getMessage());
}
return sb.toString();
}
use of java.io.UnsupportedEncodingException in project OpenAM by OpenRock.
the class DataEncryptor method encryptWithAsymmetricKey.
/**
* Encrypts the given data with an asymmetric key. The asymmetric
* encryption uses symmetric secret key for data encryption and sends
* the secret key to the recipient by encrypting the same with given
* transport key (publick key).
* @param data the data to be encrypted.
* @param encryptionAlgorithm the encryption algorithm to be used.
* The encryption algorithm must be one of the supported
* algorithm by the underlying JCE encryption provider.
* Examples of encryption algorithms are "DES", "AES" etc.
* @param encryptionStrength the encryption strength for a given
* encryption algorithm.
* @param encKey the encryption key to be used. For PKI, this
* key should be public key of the intended recipient.
* @return the encrypted data in Base64 encoded format.
*/
public static String encryptWithAsymmetricKey(String data, String encryptionAlgorithm, int encryptionStrength, Key encKey) throws Exception {
try {
KeyGenerator keygen = KeyGenerator.getInstance(encryptionAlgorithm);
if (encryptionStrength != 0) {
keygen.init(encryptionStrength);
}
SecretKey sKey = keygen.generateKey();
Cipher cipher = Cipher.getInstance(encryptionAlgorithm);
cipher.init(Cipher.ENCRYPT_MODE, sKey);
byte[] encData = cipher.doFinal(data.getBytes("UTF-8"));
cipher = Cipher.getInstance(encKey.getAlgorithm());
cipher.init(Cipher.WRAP_MODE, encKey);
byte[] keyWrap = cipher.wrap(sKey);
byte[] encDataPad = wrapKeyWithEncryptedData(encData, keyWrap);
return Base64.encode(encDataPad);
} catch (NoSuchAlgorithmException nse) {
throw new Exception(nse.getMessage());
} catch (NoSuchPaddingException npe) {
throw new Exception(npe.getMessage());
} catch (InvalidKeyException ike) {
throw new Exception(ike.getMessage());
} catch (UnsupportedEncodingException uae) {
throw new Exception(uae.getMessage());
}
}
Aggregations