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

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

Introduction

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

Prototype

String APPLICATION_X_WWW_FORM_URLENCODED_UTF8

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

Click Source Link

Usage

From source file:com.liferay.testopensocialoauthconsumer.messaging.TestOpenSocialOAuthConsumerHotDeployMessageListener.java

License:Open Source License

protected void setupGadget() throws Exception {
    Class<?> clazz = getClass();

    ClassLoader classLoader = clazz.getClassLoader();

    long companyId = PortalUtil.getDefaultCompanyId();

    User user = UserLocalServiceUtil.getDefaultUser(companyId);
    Group group = GroupLocalServiceUtil.getCompanyGroup(companyId);

    Folder gadgetEditorRootFolder = getGadgetEditorRootFolder(user.getUserId(), group.getGroupId());

    FileEntry fileEntry = null;//from  w w w. jav a  2  s. c o m

    try {
        fileEntry = DLAppLocalServiceUtil.getFileEntry(group.getGroupId(), gadgetEditorRootFolder.getFolderId(),
                _GADGET_FILE_NAME);
    } catch (NoSuchFileEntryException nsfee) {
        InputStream inputStream = classLoader
                .getResourceAsStream("/resources/gadgets/".concat(_GADGET_FILE_NAME));

        String xml = new String(FileUtil.getBytes(inputStream));

        xml = StringUtil.replace(xml,
                new String[] { "[$ACCESS_URL$]", "[$AUTHORIZATION_URL$]", "[$FOLDER_ID$]", "[$PORTAL_URL$]",
                        "[$REPOSITORY_ID$]", "[$REQUEST_URL$]" },
                new String[] { "/c/portal/oauth/access_token", "/c/portal/oauth/authorize",
                        String.valueOf(gadgetEditorRootFolder.getFolderId()), "http://localhost:8080",
                        String.valueOf(group.getGroupId()), "/c/portal/oauth/request_token" });

        ServiceContext serviceContext = new ServiceContext();

        serviceContext.setAddGroupPermissions(true);
        serviceContext.setAddGuestPermissions(true);
        serviceContext.setScopeGroupId(group.getGroupId());

        serviceContext.setAttribute("sourceFileName", _GADGET_FILE_NAME);

        fileEntry = DLAppLocalServiceUtil.addFileEntry(user.getUserId(),
                gadgetEditorRootFolder.getRepositoryId(), gadgetEditorRootFolder.getFolderId(),
                _GADGET_FILE_NAME, ContentTypes.APPLICATION_X_WWW_FORM_URLENCODED_UTF8, _GADGET_FILE_NAME,
                StringPool.BLANK, StringPool.BLANK, xml.getBytes(StringPool.UTF8), serviceContext);
    }

    Gadget gadget = GadgetLocalServiceUtil.fetchGadget(companyId, getFileEntryURL(fileEntry));

    if (gadget == null) {
        ServiceContext serviceContext = new ServiceContext();

        serviceContext.setAddGroupPermissions(true);
        serviceContext.setAddGuestPermissions(true);
        serviceContext.setScopeGroupId(group.getGroupId());

        GadgetLocalServiceUtil.addGadget(companyId, getFileEntryURL(fileEntry), _PORTLET_CATEGORY_NAMES,
                serviceContext);
    }
}

From source file:com.twelve.capital.external.feed.util.HttpImpl.java

License:Open Source License

protected byte[] URLtoByteArray(String location, Http.Method method, Map<String, String> headers,
        Cookie[] cookies, Http.Auth auth, Http.Body body, List<Http.FilePart> fileParts,
        Map<String, String> parts, Http.Response response, boolean followRedirects, String progressId,
        PortletRequest portletRequest) throws IOException {

    byte[] bytes = null;

    HttpMethod httpMethod = null;//from w  ww  .  j a v  a  2  s.c om
    HttpState httpState = null;

    try {
        _cookies.set(null);

        if (location == null) {
            return null;
        } else if (!location.startsWith(Http.HTTP_WITH_SLASH) && !location.startsWith(Http.HTTPS_WITH_SLASH)) {

            location = Http.HTTP_WITH_SLASH + location;
        }

        HostConfiguration hostConfiguration = getHostConfiguration(location);

        HttpClient httpClient = getClient(hostConfiguration);

        if (method.equals(Http.Method.POST) || method.equals(Http.Method.PUT)) {

            if (method.equals(Http.Method.POST)) {
                httpMethod = new PostMethod(location);
            } else {
                httpMethod = new PutMethod(location);
            }

            if (body != null) {
                RequestEntity requestEntity = new StringRequestEntity(body.getContent(), body.getContentType(),
                        body.getCharset());

                EntityEnclosingMethod entityEnclosingMethod = (EntityEnclosingMethod) httpMethod;

                entityEnclosingMethod.setRequestEntity(requestEntity);
            } else if (method.equals(Http.Method.POST)) {
                PostMethod postMethod = (PostMethod) httpMethod;

                if (!hasRequestHeader(postMethod, HttpHeaders.CONTENT_TYPE)) {

                    HttpClientParams httpClientParams = httpClient.getParams();

                    httpClientParams.setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, StringPool.UTF8);
                }

                processPostMethod(postMethod, fileParts, parts);
            }
        } else if (method.equals(Http.Method.DELETE)) {
            httpMethod = new DeleteMethod(location);
        } else if (method.equals(Http.Method.HEAD)) {
            httpMethod = new HeadMethod(location);
        } else {
            httpMethod = new GetMethod(location);
        }

        if (headers != null) {
            for (Map.Entry<String, String> header : headers.entrySet()) {
                httpMethod.addRequestHeader(header.getKey(), header.getValue());
            }
        }

        if ((method.equals(Http.Method.POST) || method.equals(Http.Method.PUT)) && ((body != null)
                || ((fileParts != null) && !fileParts.isEmpty()) || ((parts != null) && !parts.isEmpty()))) {
        } else if (!hasRequestHeader(httpMethod, HttpHeaders.CONTENT_TYPE)) {
            httpMethod.addRequestHeader(HttpHeaders.CONTENT_TYPE,
                    ContentTypes.APPLICATION_X_WWW_FORM_URLENCODED_UTF8);
        }

        if (!hasRequestHeader(httpMethod, HttpHeaders.USER_AGENT)) {
            httpMethod.addRequestHeader(HttpHeaders.USER_AGENT, _DEFAULT_USER_AGENT);
        }

        httpState = new HttpState();

        if (ArrayUtil.isNotEmpty(cookies)) {
            org.apache.commons.httpclient.Cookie[] commonsCookies = toCommonsCookies(cookies);

            httpState.addCookies(commonsCookies);

            HttpMethodParams httpMethodParams = httpMethod.getParams();

            httpMethodParams.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
        }

        if (auth != null) {
            httpMethod.setDoAuthentication(true);

            httpState.setCredentials(new AuthScope(auth.getHost(), auth.getPort(), auth.getRealm()),
                    new UsernamePasswordCredentials(auth.getUsername(), auth.getPassword()));
        }

        proxifyState(httpState, hostConfiguration);

        int responseCode = httpClient.executeMethod(hostConfiguration, httpMethod, httpState);

        response.setResponseCode(responseCode);

        Header locationHeader = httpMethod.getResponseHeader("location");

        if ((locationHeader != null) && !locationHeader.equals(location)) {
            String redirect = locationHeader.getValue();

            if (followRedirects) {
                return URLtoByteArray(redirect, Http.Method.GET, headers, cookies, auth, body, fileParts, parts,
                        response, followRedirects, progressId, portletRequest);
            } else {
                response.setRedirect(redirect);
            }
        }

        InputStream inputStream = httpMethod.getResponseBodyAsStream();

        if (inputStream != null) {
            int contentLength = 0;

            Header contentLengthHeader = httpMethod.getResponseHeader(HttpHeaders.CONTENT_LENGTH);

            if (contentLengthHeader != null) {
                contentLength = GetterUtil.getInteger(contentLengthHeader.getValue());

                response.setContentLength(contentLength);
            }

            Header contentType = httpMethod.getResponseHeader(HttpHeaders.CONTENT_TYPE);

            if (contentType != null) {
                response.setContentType(contentType.getValue());
            }

            if (Validator.isNotNull(progressId) && (portletRequest != null)) {

                ProgressInputStream progressInputStream = new ProgressInputStream(portletRequest, inputStream,
                        contentLength, progressId);

                UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream(
                        contentLength);

                try {
                    progressInputStream.readAll(unsyncByteArrayOutputStream);
                } finally {
                    progressInputStream.clearProgress();
                }

                bytes = unsyncByteArrayOutputStream.unsafeGetByteArray();

                unsyncByteArrayOutputStream.close();
            } else {
                bytes = FileUtil.getBytes(inputStream);
            }
        }

        for (Header header : httpMethod.getResponseHeaders()) {
            response.addHeader(header.getName(), header.getValue());
        }

        return bytes;
    } finally {
        try {
            if (httpState != null) {
                _cookies.set(toServletCookies(httpState.getCookies()));
            }
        } catch (Exception e) {
            _log.error(e, e);
        }

        try {
            if (httpMethod != null) {
                httpMethod.releaseConnection();
            }
        } catch (Exception e) {
            _log.error(e, e);
        }
    }
}