Example usage for com.liferay.portal.kernel.util ContentTypes APPLICATION_X_JAVA_SERIALIZED_OBJECT

List of usage examples for com.liferay.portal.kernel.util ContentTypes APPLICATION_X_JAVA_SERIALIZED_OBJECT

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.util ContentTypes APPLICATION_X_JAVA_SERIALIZED_OBJECT.

Prototype

String APPLICATION_X_JAVA_SERIALIZED_OBJECT

To view the source code for com.liferay.portal.kernel.util ContentTypes APPLICATION_X_JAVA_SERIALIZED_OBJECT.

Click Source Link

Usage

From source file:org.kmworks.liferay.rpc.utils.RPCTunnelUtil.java

License:Open Source License

private static HttpURLConnection _getConnection(String baseUrl, String login, String password)
        throws IOException {

    checkNotNull(baseUrl);/*ww w . j a  v a2  s .  c om*/
    checkNotNull(login);
    checkNotNull(password);

    ///-URL url = new URL(httpPrincipal.getUrl() + "/api/liferay/do");
    final URL url = new URL(baseUrl + "/rpc-server-hook/api/do"); ///+
    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
    httpURLConnection.setDoInput(true);
    httpURLConnection.setDoOutput(true);

    if (!_VERIFY_SSL_HOSTNAME && httpURLConnection instanceof HttpsURLConnection) {
        HttpsURLConnection httpsURLConnection = (HttpsURLConnection) httpURLConnection;

        httpsURLConnection.setHostnameVerifier(new HostnameVerifier() {

            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
    }

    httpURLConnection.setRequestProperty(HttpHeaders.CONTENT_TYPE,
            ContentTypes.APPLICATION_X_JAVA_SERIALIZED_OBJECT);
    httpURLConnection.setUseCaches(false);
    httpURLConnection.setRequestMethod(HttpMethods.POST);

    final String userNameAndPassword = login + StringPool.COLON + password;
    httpURLConnection.setRequestProperty(HttpHeaders.AUTHORIZATION,
            HttpServletRequest.BASIC_AUTH + StringPool.SPACE + Base64.encode(userNameAndPassword.getBytes()));

    return httpURLConnection;
}

From source file:org.kmworks.portal.rpc.utils.RPCTunnelUtil.java

License:Open Source License

public static Object invoke(HttpPrincipal httpPrincipal, MethodHandler methodHandler) throws Exception {

    HttpPrincipal principal = AuthToken.clonePrincipal(httpPrincipal);
    principal.setPassword(Encryptor.encrypt(getSharedSecretKey(), principal.getLogin()));

    final HttpURLConnection httpURLConnection = HttpUtil.getConnection(
            principal.getUrl() + "/rpc-server-hook/rpc/api/do", principal.getLogin(), principal.getPassword(),
            ContentTypes.APPLICATION_X_JAVA_SERIALIZED_OBJECT);

    try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(httpURLConnection.getOutputStream())) {
        ObjectValuePair<HttpPrincipal, MethodHandler> ovp = new ObjectValuePair<>(httpPrincipal, methodHandler);
        objectOutputStream.writeObject(ovp);
        objectOutputStream.flush();/*from  w ww.j  a va2  s. c om*/
    }

    Object returnObject = null;
    try (ObjectInputStream objectInputStream = new ObjectInputStream(httpURLConnection.getInputStream())) {
        returnObject = objectInputStream.readObject();
    } catch (EOFException eofe) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Unable to read object from object stream", eofe);
        }
    } catch (IOException ioe) {
        String ioeMessage = ioe.getMessage();
        if (ioeMessage != null && ioeMessage.contains("HTTP response code: 401")) {
            throw new PrincipalException(ioeMessage);
        } else {
            throw ioe;
        }
    }

    if (returnObject != null && returnObject instanceof Exception) {
        throw (Exception) returnObject;
    }

    return returnObject;
}