List of usage examples for org.apache.commons.httpclient HttpStatus SC_UNSUPPORTED_MEDIA_TYPE
int SC_UNSUPPORTED_MEDIA_TYPE
To view the source code for org.apache.commons.httpclient HttpStatus SC_UNSUPPORTED_MEDIA_TYPE.
Click Source Link
From source file:org.alfresco.integrations.google.docs.service.GoogleDocsServiceImpl.java
private InputStream getFileInputStream(DriveFile driveFile, String mimetype) throws GoogleDocsAuthenticationException, GoogleDocsRefreshTokenException, GoogleDocsServiceException { RestTemplate restTemplate = new RestTemplate(); String url = getExportLink(driveFile, mimetype); log.debug("Google Export Format (mimetype) link: " + url); if (url == null) { throw new GoogleDocsServiceException("Google Docs Export Format not found.", HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE); }/*from w w w .ja v a 2s .c om*/ HttpHeaders headers = new HttpHeaders(); headers.add("Authorization", "Bearer " + getConnection().getApi().getAccessToken()); MultiValueMap<String, Object> body = new LinkedMultiValueMap<String, Object>(); HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<MultiValueMap<String, Object>>(body, headers); ResponseEntity<byte[]> response = restTemplate.exchange(url, HttpMethod.GET, entity, byte[].class); return new ByteArrayInputStream(response.getBody()); }
From source file:org.alfresco.integrations.google.docs.webscripts.CreateContent.java
@Override protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) { // Set Service Beans this.getGoogleDocsServiceSubsystem(); Map<String, Object> model = new HashMap<String, Object>(); if (googledocsService.isEnabled()) { String contentType = req.getParameter(PARAM_TYPE); NodeRef parentNodeRef = new NodeRef(req.getParameter(PARAM_PARENT)); log.debug("ContentType: " + contentType + "; Parent: " + parentNodeRef); NodeRef newNode = null;// w w w . j a va 2 s.com File file = null; try { Credential credential = googledocsService.getCredential(); if (contentType.equals(GoogleDocsConstants.DOCUMENT_TYPE)) { newNode = createFile(parentNodeRef, contentType, GoogleDocsConstants.MIMETYPE_DOCUMENT); file = googledocsService.createDocument(credential, newNode); } else if (contentType.equals(GoogleDocsConstants.SPREADSHEET_TYPE)) { newNode = createFile(parentNodeRef, contentType, GoogleDocsConstants.MIMETYPE_SPREADSHEET); file = googledocsService.createSpreadSheet(credential, newNode); } else if (contentType.equals(GoogleDocsConstants.PRESENTATION_TYPE)) { newNode = createFile(parentNodeRef, contentType, GoogleDocsConstants.MIMETYPE_PRESENTATION); file = googledocsService.createPresentation(credential, newNode); } else { throw new WebScriptException(HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE, "Content Type Not Found."); } googledocsService.decorateNode(newNode, file, googledocsService.getLatestRevision(credential, file), true); } catch (GoogleDocsServiceException gdse) { if (gdse.getPassedStatusCode() > -1) { throw new WebScriptException(gdse.getPassedStatusCode(), gdse.getMessage()); } else { throw new WebScriptException(gdse.getMessage()); } } catch (GoogleDocsTypeException gdte) { throw new WebScriptException(HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE, gdte.getMessage()); } catch (GoogleDocsAuthenticationException gdae) { throw new WebScriptException(HttpStatus.SC_BAD_GATEWAY, gdae.getMessage()); } catch (GoogleDocsRefreshTokenException gdrte) { throw new WebScriptException(HttpStatus.SC_BAD_GATEWAY, gdrte.getMessage()); } catch (IOException ioe) { throw new WebScriptException(HttpStatus.SC_INTERNAL_SERVER_ERROR, ioe.getMessage(), ioe); } catch (Exception e) { throw new WebScriptException(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage(), e); } googledocsService.lockNode(newNode); model.put(MODEL_NODEREF, newNode.toString()); model.put(MODEL_EDITOR_URL, file.getAlternateLink()); } else { throw new WebScriptException(HttpStatus.SC_SERVICE_UNAVAILABLE, "Google Docs Disabled"); } return model; }
From source file:org.alfresco.integrations.google.docs.webscripts.SaveContent.java
@Override protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) { getGoogleDocsServiceSubsystem();//from ww w. j a v a 2 s. c om Map<String, Object> model = new HashMap<String, Object>(); boolean success = false; Map<String, Serializable> map = parseContent(req); final NodeRef nodeRef = (NodeRef) map.get(JSON_KEY_NODEREF); log.debug("Saving Node to Alfresco from Google: " + nodeRef); try { SiteInfo siteInfo = siteService.getSite(nodeRef); if (siteInfo == null || siteService.isMember(siteInfo.getShortName(), AuthenticationUtil.getRunAsUser())) { if (!(Boolean) map.get(JSON_KEY_OVERRIDE)) { log.debug("Check for Concurent Users."); if (googledocsService.hasConcurrentEditors(nodeRef)) { throw new WebScriptException(HttpStatus.SC_CONFLICT, "Node: " + nodeRef.toString() + " has concurrent editors."); } } // Should the content be removed from the users Google Drive Account boolean removeFromDrive = (map.get(JSON_KEY_REMOVEFROMDRIVE) != null) ? (Boolean) map.get(JSON_KEY_REMOVEFROMDRIVE) : true; String contentType = googledocsService.getContentType(nodeRef); log.debug("NodeRef: " + nodeRef + "; ContentType: " + contentType); if (contentType.equals(GoogleDocsConstants.DOCUMENT_TYPE)) { if (googledocsService.isGoogleDocsLockOwner(nodeRef)) { googledocsService.unlockNode(nodeRef); if (removeFromDrive) { googledocsService.getDocument(nodeRef); } else { googledocsService.getDocument(nodeRef, removeFromDrive); } success = true; // TODO Make getDocument return boolean } else { throw new WebScriptException(HttpStatus.SC_FORBIDDEN, "Document is locked by another user."); } } else if (contentType.equals(GoogleDocsConstants.SPREADSHEET_TYPE)) { if (googledocsService.isGoogleDocsLockOwner(nodeRef)) { googledocsService.unlockNode(nodeRef); if (removeFromDrive) { googledocsService.getSpreadSheet(nodeRef); } else { googledocsService.getSpreadSheet(nodeRef, removeFromDrive); } success = true; // TODO Make getSpreadsheet return // boolean } else { throw new WebScriptException(HttpStatus.SC_FORBIDDEN, "Document is locked by another user."); } } else if (contentType.equals(GoogleDocsConstants.PRESENTATION_TYPE)) { if (googledocsService.isGoogleDocsLockOwner(nodeRef)) { googledocsService.unlockNode(nodeRef); if (removeFromDrive) { googledocsService.getPresentation(nodeRef); } else { googledocsService.getPresentation(nodeRef, removeFromDrive); } success = true; // TODO Make getPresentation return // boolean } else { throw new WebScriptException(HttpStatus.SC_FORBIDDEN, "Document is locked by another user."); } } else { throw new WebScriptException(HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE, "Content Type: " + contentType + " unknown."); } // Finish this off with a version create or update Map<String, Serializable> versionProperties = new HashMap<String, Serializable>(); if (nodeService.hasAspect(nodeRef, ContentModel.ASPECT_VERSIONABLE)) { versionProperties.put(Version2Model.PROP_VERSION_TYPE, map.get(JSON_KEY_MAJORVERSION)); versionProperties.put(Version2Model.PROP_DESCRIPTION, map.get(JSON_KEY_DESCRIPTION)); } else { versionProperties.put(Version2Model.PROP_VERSION_TYPE, VersionType.MAJOR); nodeService.setProperty(nodeRef, ContentModel.PROP_AUTO_VERSION, true); nodeService.setProperty(nodeRef, ContentModel.PROP_AUTO_VERSION_PROPS, true); } log.debug("Version Node:" + nodeRef + "; Version Properties: " + versionProperties); Version version = versionService.createVersion(nodeRef, versionProperties); model.put(MODEL_VERSION, version.getVersionLabel()); if (!removeFromDrive) { googledocsService.lockNode(nodeRef); } } else { throw new AccessDeniedException( "Access Denied. You do not have the appropriate permissions to perform this operation."); } } catch (GoogleDocsAuthenticationException gdae) { throw new WebScriptException(HttpStatus.SC_BAD_GATEWAY, gdae.getMessage()); } catch (GoogleDocsServiceException gdse) { if (gdse.getPassedStatusCode() > -1) { throw new WebScriptException(gdse.getPassedStatusCode(), gdse.getMessage()); } else { throw new WebScriptException(gdse.getMessage()); } } catch (GoogleDocsRefreshTokenException gdrte) { throw new WebScriptException(HttpStatus.SC_BAD_GATEWAY, gdrte.getMessage()); } catch (ConstraintException ce) { throw new WebScriptException(GoogleDocsConstants.STATUS_INTEGIRTY_VIOLATION, ce.getMessage(), ce); } catch (AccessDeniedException ade) { // This code will make changes after the rollback has occurred to clean up the node (remove the lock and the Google Docs // aspect AlfrescoTransactionSupport.bindListener(new TransactionListenerAdapter() { public void afterRollback() { log.debug("Rollback Save to node: " + nodeRef); transactionService.getRetryingTransactionHelper() .doInTransaction(new RetryingTransactionCallback<Object>() { public Object execute() throws Throwable { return AuthenticationUtil .runAsSystem(new AuthenticationUtil.RunAsWork<Object>() { public Object doWork() throws Exception { googledocsService.unlockNode(nodeRef); googledocsService.unDecorateNode(nodeRef); // If the node was just created ('Create Content') it will have the temporary aspect and should // be completely removed. if (nodeService.hasAspect(nodeRef, ContentModel.ASPECT_TEMPORARY)) { nodeService.deleteNode(nodeRef); } return null; } }); } }, false, true); } }); throw new WebScriptException(HttpStatus.SC_FORBIDDEN, ade.getMessage(), ade); } catch (Exception e) { throw new WebScriptException(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage(), e); } model.put(MODEL_SUCCESS, success); return model; }
From source file:org.broadleafcommerce.core.web.api.BroadleafSpringRestExceptionMapper.java
@ExceptionHandler(HttpMediaTypeNotSupportedException.class) public @ResponseBody ErrorWrapper handleHttpMediaTypeNotSupportedException(HttpServletRequest request, HttpServletResponse response, Exception ex) { ErrorWrapper errorWrapper = (ErrorWrapper) context.getBean(ErrorWrapper.class.getName()); Locale locale = null;/* w ww .ja va2s . c o m*/ BroadleafRequestContext requestContext = BroadleafRequestContext.getBroadleafRequestContext(); if (requestContext != null) { locale = requestContext.getJavaLocale(); } LOG.error("An error occured invoking a REST service", ex); if (locale == null) { locale = Locale.getDefault(); } errorWrapper.setHttpStatusCode(HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE); response.setStatus(resolveResponseStatusCode(ex, errorWrapper)); ErrorMessageWrapper errorMessageWrapper = (ErrorMessageWrapper) context .getBean(ErrorMessageWrapper.class.getName()); errorMessageWrapper .setMessageKey(resolveClientMessageKey(BroadleafWebServicesException.CONTENT_TYPE_NOT_SUPPORTED)); errorMessageWrapper.setMessage(messageSource.getMessage( BroadleafWebServicesException.CONTENT_TYPE_NOT_SUPPORTED, new String[] { request.getContentType() }, BroadleafWebServicesException.CONTENT_TYPE_NOT_SUPPORTED, locale)); errorWrapper.getMessages().add(errorMessageWrapper); return errorWrapper; }
From source file:org.eclipse.lyo.testsuite.oslcv2.CreationAndUpdateBaseTests.java
protected void updateCreatedResourceWithBadType(String contentType, String accept, String createContent, String updateContent, String badType) throws IOException { HttpResponse resp = createResource(contentType, accept, createContent); Header location = getRequiredLocationHeader(resp); Header eTag = resp.getFirstHeader("ETag"); Header lastModified = resp.getFirstHeader("Last-Modified"); // Ignore eTag and Last-Modified for this test int size = headers.length; if (eTag != null) size++;/*from w ww . j a va 2 s. c om*/ if (lastModified != null) size++; Header[] putHeaders = new Header[size]; int i = 0; for (; i < headers.length; i++) { putHeaders[i] = headers[i]; } if (eTag != null) { putHeaders[i++] = new BasicHeader("If-Match", eTag.getValue()); } if (lastModified != null) { putHeaders[i++] = new BasicHeader("If-Unmodified-Since", lastModified.getValue()); } // Now, go to the url of the new change request and update it. resp = OSLCUtils.putDataToUrl(location.getValue(), basicCreds, "*/*", badType, updateContent, putHeaders); if (resp != null && resp.getEntity() != null) EntityUtils.consume(resp.getEntity()); assertEquals(HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE, resp.getStatusLine().getStatusCode()); // Clean up after the test by attempting to delete the created resource if (location != null) resp = OSLCUtils.deleteFromUrl(location.getValue(), basicCreds, ""); if (resp != null && resp.getEntity() != null) EntityUtils.consume(resp.getEntity()); }
From source file:org.opens.tanaguru.util.http.HttpRequestHandler.java
private int computeStatus(int status) { switch (status) { case HttpStatus.SC_FORBIDDEN: case HttpStatus.SC_METHOD_NOT_ALLOWED: case HttpStatus.SC_BAD_REQUEST: case HttpStatus.SC_UNAUTHORIZED: case HttpStatus.SC_PAYMENT_REQUIRED: case HttpStatus.SC_NOT_FOUND: case HttpStatus.SC_NOT_ACCEPTABLE: case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED: case HttpStatus.SC_REQUEST_TIMEOUT: case HttpStatus.SC_CONFLICT: case HttpStatus.SC_GONE: case HttpStatus.SC_LENGTH_REQUIRED: case HttpStatus.SC_PRECONDITION_FAILED: case HttpStatus.SC_REQUEST_TOO_LONG: case HttpStatus.SC_REQUEST_URI_TOO_LONG: case HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE: case HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE: case HttpStatus.SC_EXPECTATION_FAILED: case HttpStatus.SC_INSUFFICIENT_SPACE_ON_RESOURCE: case HttpStatus.SC_METHOD_FAILURE: case HttpStatus.SC_UNPROCESSABLE_ENTITY: case HttpStatus.SC_LOCKED: case HttpStatus.SC_FAILED_DEPENDENCY: case HttpStatus.SC_INTERNAL_SERVER_ERROR: case HttpStatus.SC_NOT_IMPLEMENTED: case HttpStatus.SC_BAD_GATEWAY: case HttpStatus.SC_SERVICE_UNAVAILABLE: case HttpStatus.SC_GATEWAY_TIMEOUT: case HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED: case HttpStatus.SC_INSUFFICIENT_STORAGE: return 0; case HttpStatus.SC_CONTINUE: case HttpStatus.SC_SWITCHING_PROTOCOLS: case HttpStatus.SC_PROCESSING: case HttpStatus.SC_OK: case HttpStatus.SC_CREATED: case HttpStatus.SC_ACCEPTED: case HttpStatus.SC_NON_AUTHORITATIVE_INFORMATION: case HttpStatus.SC_NO_CONTENT: case HttpStatus.SC_RESET_CONTENT: case HttpStatus.SC_PARTIAL_CONTENT: case HttpStatus.SC_MULTI_STATUS: case HttpStatus.SC_MULTIPLE_CHOICES: case HttpStatus.SC_MOVED_PERMANENTLY: case HttpStatus.SC_MOVED_TEMPORARILY: case HttpStatus.SC_SEE_OTHER: case HttpStatus.SC_NOT_MODIFIED: case HttpStatus.SC_USE_PROXY: case HttpStatus.SC_TEMPORARY_REDIRECT: return 1; default://from ww w . j a v a 2 s .co m return 1; } }