Example usage for com.liferay.portal.kernel.servlet HttpHeaders CONTENT_TYPE

List of usage examples for com.liferay.portal.kernel.servlet HttpHeaders CONTENT_TYPE

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.servlet HttpHeaders CONTENT_TYPE.

Prototype

String CONTENT_TYPE

To view the source code for com.liferay.portal.kernel.servlet HttpHeaders CONTENT_TYPE.

Click Source Link

Usage

From source file:com.liferay.compat.hook.webdav.CompatDLWebDAVStorageImpl.java

License:Open Source License

public Status lockResource(WebDAVRequest webDAVRequest, String owner, long timeout) throws WebDAVException {

    Resource resource = getResource(webDAVRequest);

    Lock lock = null;//from   ww w  . j a  v a  2  s.  c om
    int status = HttpServletResponse.SC_OK;

    try {
        if (resource == null) {
            status = HttpServletResponse.SC_CREATED;

            HttpServletRequest request = webDAVRequest.getHttpServletRequest();

            String[] pathArray = webDAVRequest.getPathArray();

            long companyId = webDAVRequest.getCompanyId();
            long groupId = webDAVRequest.getGroupId();
            long parentFolderId = getParentFolderId(companyId, pathArray);
            String title = WebDAVUtil.getResourceName(pathArray);

            String contentType = GetterUtil.get(request.getHeader(HttpHeaders.CONTENT_TYPE),
                    ContentTypes.APPLICATION_OCTET_STREAM);

            if (contentType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
                contentType = MimeTypesUtil.getContentType(request.getInputStream(), title);
            }

            String description = StringPool.BLANK;
            String changeLog = StringPool.BLANK;

            File file = FileUtil.createTempFile(FileUtil.getExtension(title));

            file.createNewFile();

            ServiceContext serviceContext = new ServiceContext();

            serviceContext.setAddGroupPermissions(isAddGroupPermissions(groupId));
            serviceContext.setAddGuestPermissions(true);

            FileEntry fileEntry = DLAppServiceUtil.addFileEntry(groupId, parentFolderId, title, contentType,
                    title, description, changeLog, file, serviceContext);

            resource = toResource(webDAVRequest, fileEntry, false);
        }

        if (isInstanceOfDLFileEntryResourceImpl(super.getResource(webDAVRequest))) {

            FileEntry fileEntry = (FileEntry) resource.getModel();

            ServiceContext serviceContext = new ServiceContext();

            serviceContext.setAttribute(DLUtil.MANUAL_CHECK_IN_REQUIRED,
                    CompatWebDAVThreadLocal.isManualCheckInRequired());

            DLAppServiceUtil.checkOutFileEntry(fileEntry.getFileEntryId(), owner, timeout, serviceContext);

            lock = fileEntry.getLock();
        } else {
            boolean inheritable = false;

            long depth = WebDAVUtil.getDepth(webDAVRequest.getHttpServletRequest());

            if (depth != 0) {
                inheritable = true;
            }

            Folder folder = (Folder) resource.getModel();

            lock = DLAppServiceUtil.lockFolder(folder.getRepositoryId(), folder.getFolderId(), owner,
                    inheritable, timeout);
        }
    } catch (Exception e) {

        // DuplicateLock is 423 not 501

        if (!(e instanceof DuplicateLockException)) {
            throw new WebDAVException(e);
        }

        status = WebDAVUtil.SC_LOCKED;
    }

    return new Status(lock, status);
}

From source file:com.liferay.document.library.web.internal.webdav.DLWebDAVStorageImpl.java

License:Open Source License

protected String getContentType(HttpServletRequest request, File file, String title, String extension) {

    String contentType = GetterUtil.getString(request.getHeader(HttpHeaders.CONTENT_TYPE),
            ContentTypes.APPLICATION_OCTET_STREAM);

    if (contentType.equals(ContentTypes.APPLICATION_OCTET_STREAM)
            || contentType.equals(MS_OFFICE_2010_TEXT_XML_UTF8)) {

        contentType = MimeTypesUtil.getContentType(file, title);
    }//from  ww  w.  ja va 2s.  c om

    return contentType;
}

From source file:com.liferay.document.library.webdav.test.BaseWebDAVTestCase.java

License:Open Source License

public Tuple service(String method, String path, Map<String, String> headers, byte[] data) {

    WebDAVStorage webDAVStorage = new DLWebDAVStorageImpl();

    webDAVStorage.setToken("document_library");

    WebDAVUtil.addStorage(webDAVStorage);

    WebDAVServlet webDAVServlet = new WebDAVServlet();

    String requestURI = _CONTEXT_PATH + _SERVLET_PATH + _PATH_INFO_PREFACE + path;

    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(method, requestURI);

    mockHttpServletRequest.setContextPath(_CONTEXT_PATH);
    mockHttpServletRequest.setServletPath(_SERVLET_PATH);
    mockHttpServletRequest.setPathInfo(_PATH_INFO_PREFACE + path);

    try {/*from   w w  w  . j  a v a 2s .  c om*/
        mockHttpServletRequest.setRemoteUser(String.valueOf(TestPropsValues.getUserId()));
    } catch (Exception e) {
        Assert.fail("User ID cannot be initialized");
    }

    if (headers == null) {
        headers = new HashMap<>();
    }

    headers.put(HttpHeaders.USER_AGENT, getUserAgent());

    try {
        throw new Exception();
    } catch (Exception e) {
        StackTraceElement[] stackTraceElements = e.getStackTrace();

        for (StackTraceElement stackTraceElement : stackTraceElements) {
            String methodName = stackTraceElement.getMethodName();

            if (methodName.equals("setUp") || methodName.equals("tearDown") || methodName.startsWith("test")) {

                String testName = StringUtil.extractLast(stackTraceElement.getClassName(), CharPool.PERIOD);

                testName = StringUtil.removeSubstrings(testName, "WebDAV", "Test");

                headers.put("X-Litmus", testName + ": (" + stackTraceElement.getMethodName() + ":"
                        + stackTraceElement.getLineNumber() + ")");

                break;
            }
        }
    }

    if (data != null) {
        mockHttpServletRequest.setContent(data);

        String contentType = headers.remove(HttpHeaders.CONTENT_TYPE);

        if (contentType != null) {
            mockHttpServletRequest.setContentType(contentType);
        } else {
            mockHttpServletRequest.setContentType(ContentTypes.TEXT_PLAIN);
        }
    }

    for (Map.Entry<String, String> entry : headers.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();

        mockHttpServletRequest.addHeader(key, value);
    }

    try {
        MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();

        webDAVServlet.service(mockHttpServletRequest, mockHttpServletResponse);

        int statusCode = mockHttpServletResponse.getStatus();
        byte[] responseBody = mockHttpServletResponse.getContentAsByteArray();

        Map<String, String> responseHeaders = new HashMap<>();

        for (String name : mockHttpServletResponse.getHeaderNames()) {
            responseHeaders.put(name, mockHttpServletResponse.getHeader(name));
        }

        return new Tuple(statusCode, responseBody, responseHeaders);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:com.liferay.document.library.webdav.test.WebDAVLitmusBasicTest.java

License:Open Source License

@Test
public void test10MkcolWithBody() {
    Map<String, String> headers = new HashMap<>();

    headers.put(HttpHeaders.CONTENT_TYPE, "xyz-foo/bar-512");

    assertCode(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE,
            service(Method.MKCOL, "mkcolbody", headers, _TEST_CONTENT.getBytes()));
}

From source file:com.liferay.jbi.mule.http.MuleBindingServlet.java

License:Open Source License

public void service(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

    String contentType = request.getHeader(HttpHeaders.CONTENT_TYPE);

    if (_log.isDebugEnabled()) {
        _log.debug("Content type " + contentType);
    }/*from   w  w w.  ja va  2s .  c  o  m*/

    if ((contentType != null) && (contentType.startsWith(ContentTypes.MULTIPART_FORM_DATA))) {

        request = PortalUtil.getUploadServletRequest(request);
    }

    try {
        MuleClient client = new MuleClient();

        Map payload = new HashMap();

        Enumeration enu = request.getParameterNames();

        while (enu.hasMoreElements()) {
            try {
                String name = (String) enu.nextElement();

                String value = request.getParameter(name);

                payload.put(name, value);
            } catch (Exception e) {
            }
        }

        UMOMessage message = client.send(_endpoint, payload, null);

        String result = (String) message.getPayload();

        response.setContentType("text/xml");

        try {
            ServletResponseUtil.write(response, result);
        } catch (Exception e) {
            if (_log.isWarnEnabled()) {
                _log.warn(e, e);
            }
        }
    } catch (UMOException ue) {
    }
}

From source file:com.liferay.jbi.servicemix.http.SpringBindingServlet.java

License:Open Source License

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

    String contentType = request.getHeader(HttpHeaders.CONTENT_TYPE);

    if (_log.isDebugEnabled()) {
        _log.debug("Content type " + contentType);
    }/*w w  w  . ja  va  2 s.c  o  m*/

    if ((contentType != null) && (contentType.startsWith(ContentTypes.MULTIPART_FORM_DATA))) {

        request = PortalUtil.getUploadServletRequest(request);
    }

    try {
        getBinding().process(request, response);
    } catch (JBIException e) {
        throw new ServletException(e);
    }
}

From source file:com.liferay.jbpm.servlet.JBPMServlet.java

License:Open Source License

public void service(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

    try {/*from  ww  w .ja  va  2 s  .c o m*/
        String contentType = request.getHeader(HttpHeaders.CONTENT_TYPE);

        if (_log.isDebugEnabled()) {
            _log.debug("Content type " + contentType);
        }

        if ((contentType != null) && (contentType.startsWith(ContentTypes.MULTIPART_FORM_DATA))) {

            request = PortalUtil.getUploadServletRequest(request);
        }

        WorkflowComponentImpl workflowComponentImpl = new WorkflowComponentImpl();

        String result = workflowComponentImpl.process(request);

        response.setContentType("text/xml");

        ServletResponseUtil.write(response, result);
    } catch (Exception e) {
        _log.error(e, e);

        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
    }
}

From source file:com.liferay.marketplace.store.web.internal.portlet.RemoteMVCPortlet.java

License:Open Source License

protected void remoteProcessAction(ActionRequest actionRequest, ActionResponse actionResponse)
        throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

    OAuthRequest oAuthRequest = new OAuthRequest(Verb.POST, getServerPortletURL());

    setRequestParameters(actionRequest, actionResponse, oAuthRequest);

    addOAuthParameter(oAuthRequest, "p_p_lifecycle", "1");
    addOAuthParameter(oAuthRequest, "p_p_state", WindowState.NORMAL.toString());

    Response response = getResponse(themeDisplay.getUser(), oAuthRequest);

    if (response.getCode() == HttpServletResponse.SC_FOUND) {
        String redirectLocation = response.getHeader(HttpHeaders.LOCATION);

        actionResponse.sendRedirect(redirectLocation);
    } else {/*from w  w  w  . ja  v a2  s. com*/
        HttpServletResponse httpServletResponse = PortalUtil.getHttpServletResponse(actionResponse);

        httpServletResponse.setContentType(response.getHeader(HttpHeaders.CONTENT_TYPE));

        ServletResponseUtil.write(httpServletResponse, response.getStream());
    }
}

From source file:com.liferay.marketplace.store.web.internal.portlet.RemoteMVCPortlet.java

License:Open Source License

protected void remoteServeResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse)
        throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) resourceRequest.getAttribute(WebKeys.THEME_DISPLAY);

    OAuthRequest oAuthRequest = new OAuthRequest(Verb.GET, getServerPortletURL());

    setRequestParameters(resourceRequest, resourceResponse, oAuthRequest);

    addOAuthParameter(oAuthRequest, "p_p_lifecycle", "2");
    addOAuthParameter(oAuthRequest, "p_p_resource_id", resourceRequest.getResourceID());

    Response response = getResponse(themeDisplay.getUser(), oAuthRequest);

    String contentType = response.getHeader(HttpHeaders.CONTENT_TYPE);

    if (contentType.startsWith(ContentTypes.APPLICATION_OCTET_STREAM)) {
        String contentDisposition = response.getHeader(HttpHeaders.CONTENT_DISPOSITION);
        int contentLength = GetterUtil.getInteger(response.getHeader(HttpHeaders.CONTENT_LENGTH));

        PortletResponseUtil.sendFile(resourceRequest, resourceResponse, getFileName(contentDisposition),
                response.getStream(), contentLength, contentType, HttpHeaders.CONTENT_DISPOSITION_ATTACHMENT);
    } else {/*from  w w w .  ja v a2 s  . c  o  m*/
        resourceResponse.setContentType(contentType);

        PortletResponseUtil.write(resourceResponse, response.getStream());
    }
}

From source file:com.liferay.portlet.documentlibrary.sharepoint.DLSharepointStorageImpl.java

License:Open Source License

@Override
public void putDocument(SharepointRequest sharepointRequest) throws Exception {

    HttpServletRequest request = sharepointRequest.getHttpServletRequest();

    String documentPath = sharepointRequest.getRootPath();
    String parentFolderPath = getParentFolderPath(documentPath);

    long groupId = SharepointUtil.getGroupId(parentFolderPath);
    long parentFolderId = getLastFolderId(groupId, parentFolderPath,
            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
    String title = getResourceName(documentPath);
    String description = StringPool.BLANK;
    String changeLog = StringPool.BLANK;

    ServiceContext serviceContext = new ServiceContext();

    serviceContext.setAddGroupPermissions(true);
    serviceContext.setAddGuestPermissions(true);

    String contentType = GetterUtil.get(request.getHeader(HttpHeaders.CONTENT_TYPE),
            ContentTypes.APPLICATION_OCTET_STREAM);

    String extension = FileUtil.getExtension(title);

    File file = null;/*from   w  w w  .jav a2 s  . co  m*/

    try {
        file = FileUtil.createTempFile(extension);

        FileUtil.write(file, request.getInputStream());

        if (contentType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
            contentType = MimeTypesUtil.getContentType(file, title);
        }

        try {
            FileEntry fileEntry = getFileEntry(sharepointRequest);

            long fileEntryId = fileEntry.getFileEntryId();

            description = fileEntry.getDescription();

            String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(FileEntry.class.getName(),
                    fileEntry.getFileEntryId());

            serviceContext.setAssetTagNames(assetTagNames);

            DLAppServiceUtil.updateFileEntry(fileEntryId, title, contentType, title, description, changeLog,
                    false, file, serviceContext);
        } catch (NoSuchFileEntryException nsfee) {
            DLAppServiceUtil.addFileEntry(groupId, parentFolderId, title, contentType, title, description,
                    changeLog, file, serviceContext);
        }
    } finally {
        FileUtil.delete(file);
    }
}