@Test public void testUploadExternalProperties() throws IOException { final MessageContext msgContextMock = mock(MessageContext.class); final HttpHeaders httpHeadersMock = mock(HttpHeaders.class); final List<MediaType> mediaTypeList = new ArrayList<>(); final HttpServletRequest httpServletRequestMock = mock(HttpServletRequest.class); final HttpServletResponse httpServletResponseMock = mock(HttpServletResponse.class); when(httpHeadersMock.getAcceptableMediaTypes()).thenReturn(mediaTypeList); when(msgContextMock.getHttpHeaders()).thenReturn(httpHeadersMock); when(msgContextMock.getHttpServletRequest()).thenReturn(httpServletRequestMock); when(msgContextMock.getHttpServletResponse()).thenReturn(httpServletResponseMock); when(httpServletRequestMock.getContentType()).thenReturn("multipart/form-data; boundary=----WebKitFormBoundaryXRxegBGqTe4gApI2"); when(httpServletRequestMock.getInputStream()).thenReturn(new DelegatingServletInputStream()); cut.setMessageContext(msgContextMock); final SecurityContext securityContextMock = mock(SecurityContext.class); final AuthenticatedUser authenticatedUser = new AuthenticatedUser(securityContextMock); final Principal mockPrincipal = mock(Principal.class); when(mockPrincipal.getName()).thenReturn("user"); when(securityContextMock.getUserPrincipal()).thenReturn(mockPrincipal); cut.uploadExternalProperties(authenticatedUser); verify(impl).createResource(any(ResourceIdentifier.class), any(ResourceTemplateMetaData.class), any(InputStream.class)); }
@Override public Response uploadExternalProperties(AuthenticatedUser user) { LOGGER.info("Upload external properties by user {}", user.getUser().getId()); if (!context.getHttpHeaders().getAcceptableMediaTypes().contains(MediaType.APPLICATION_JSON_TYPE)) { context.getHttpServletResponse().setContentType(MediaType.TEXT_HTML); } ResourceIdentifier.Builder idBuilder = new ResourceIdentifier.Builder(); ResourceIdentifier resourceIdentifier = idBuilder.setResourceName(EXT_PROPERTIES_RESOURCE_NAME).build(); final ResourceTemplateMetaData metaData; InputStream data = null; CreateResourceResponseWrapper responseWrapper = null; try { metaData = resourceService.getMetaData(EXT_PROPERTIES_RESOURCE_META_DATA); ServletFileUpload sfu = new ServletFileUpload(); FileItemIterator iter = sfu.getItemIterator(context.getHttpServletRequest()); FileItemStream fileItemStream = iter.next(); data = fileItemStream.openStream(); responseWrapper = resourceService.createResource(resourceIdentifier, metaData, data); } catch (IOException ioe) { LOGGER.error("IOException thrown in uploadExternalProperties", ioe); return ResponseBuilder.notOk(Response.Status.INTERNAL_SERVER_ERROR, new FaultCodeException(FaultType.SERVICE_EXCEPTION, ioe.getMessage())); } catch (FileUploadException fue) { LOGGER.error("FileUploadException thrown in uploadExternalProperties", fue); return ResponseBuilder.notOk(Response.Status.INTERNAL_SERVER_ERROR, new FaultCodeException(FaultType.SERVICE_EXCEPTION, fue.getMessage())); } finally { assert data != null; try { data.close(); } catch (IOException e) { LOGGER.warn("IOException attempting to close external properties upload stream", e); } } return ResponseBuilder.ok(responseWrapper); }
ResourceServiceRestImpl implements ResourceServiceRest { @Override public Response uploadExternalProperties(AuthenticatedUser user) { LOGGER.info("Upload external properties by user {}", user.getUser().getId()); if (!context.getHttpHeaders().getAcceptableMediaTypes().contains(MediaType.APPLICATION_JSON_TYPE)) { context.getHttpServletResponse().setContentType(MediaType.TEXT_HTML); } ResourceIdentifier.Builder idBuilder = new ResourceIdentifier.Builder(); ResourceIdentifier resourceIdentifier = idBuilder.setResourceName(EXT_PROPERTIES_RESOURCE_NAME).build(); final ResourceTemplateMetaData metaData; InputStream data = null; CreateResourceResponseWrapper responseWrapper = null; try { metaData = resourceService.getMetaData(EXT_PROPERTIES_RESOURCE_META_DATA); ServletFileUpload sfu = new ServletFileUpload(); FileItemIterator iter = sfu.getItemIterator(context.getHttpServletRequest()); FileItemStream fileItemStream = iter.next(); data = fileItemStream.openStream(); responseWrapper = resourceService.createResource(resourceIdentifier, metaData, data); } catch (IOException ioe) { LOGGER.error("IOException thrown in uploadExternalProperties", ioe); return ResponseBuilder.notOk(Response.Status.INTERNAL_SERVER_ERROR, new FaultCodeException(FaultType.SERVICE_EXCEPTION, ioe.getMessage())); } catch (FileUploadException fue) { LOGGER.error("FileUploadException thrown in uploadExternalProperties", fue); return ResponseBuilder.notOk(Response.Status.INTERNAL_SERVER_ERROR, new FaultCodeException(FaultType.SERVICE_EXCEPTION, fue.getMessage())); } finally { assert data != null; try { data.close(); } catch (IOException e) { LOGGER.warn("IOException attempting to close external properties upload stream", e); } } return ResponseBuilder.ok(responseWrapper); } }
ResourceServiceRestImpl implements ResourceServiceRest { @Override public Response uploadExternalProperties(AuthenticatedUser user) { LOGGER.info("Upload external properties by user {}", user.getUser().getId()); if (!context.getHttpHeaders().getAcceptableMediaTypes().contains(MediaType.APPLICATION_JSON_TYPE)) { context.getHttpServletResponse().setContentType(MediaType.TEXT_HTML); } ResourceIdentifier.Builder idBuilder = new ResourceIdentifier.Builder(); ResourceIdentifier resourceIdentifier = idBuilder.setResourceName(EXT_PROPERTIES_RESOURCE_NAME).build(); final ResourceTemplateMetaData metaData; InputStream data = null; CreateResourceResponseWrapper responseWrapper = null; try { metaData = resourceService.getMetaData(EXT_PROPERTIES_RESOURCE_META_DATA); ServletFileUpload sfu = new ServletFileUpload(); FileItemIterator iter = sfu.getItemIterator(context.getHttpServletRequest()); FileItemStream fileItemStream = iter.next(); data = fileItemStream.openStream(); responseWrapper = resourceService.createResource(resourceIdentifier, metaData, data); } catch (IOException ioe) { LOGGER.error("IOException thrown in uploadExternalProperties", ioe); return ResponseBuilder.notOk(Response.Status.INTERNAL_SERVER_ERROR, new FaultCodeException(FaultType.SERVICE_EXCEPTION, ioe.getMessage())); } catch (FileUploadException fue) { LOGGER.error("FileUploadException thrown in uploadExternalProperties", fue); return ResponseBuilder.notOk(Response.Status.INTERNAL_SERVER_ERROR, new FaultCodeException(FaultType.SERVICE_EXCEPTION, fue.getMessage())); } finally { assert data != null; try { data.close(); } catch (IOException e) { LOGGER.warn("IOException attempting to close external properties upload stream", e); } } return ResponseBuilder.ok(responseWrapper); } ResourceServiceRestImpl(final ResourceService resourceService); }
ResourceServiceRestImpl implements ResourceServiceRest { @Override public Response uploadExternalProperties(AuthenticatedUser user) { LOGGER.info("Upload external properties by user {}", user.getUser().getId()); if (!context.getHttpHeaders().getAcceptableMediaTypes().contains(MediaType.APPLICATION_JSON_TYPE)) { context.getHttpServletResponse().setContentType(MediaType.TEXT_HTML); } ResourceIdentifier.Builder idBuilder = new ResourceIdentifier.Builder(); ResourceIdentifier resourceIdentifier = idBuilder.setResourceName(EXT_PROPERTIES_RESOURCE_NAME).build(); final ResourceTemplateMetaData metaData; InputStream data = null; CreateResourceResponseWrapper responseWrapper = null; try { metaData = resourceService.getMetaData(EXT_PROPERTIES_RESOURCE_META_DATA); ServletFileUpload sfu = new ServletFileUpload(); FileItemIterator iter = sfu.getItemIterator(context.getHttpServletRequest()); FileItemStream fileItemStream = iter.next(); data = fileItemStream.openStream(); responseWrapper = resourceService.createResource(resourceIdentifier, metaData, data); } catch (IOException ioe) { LOGGER.error("IOException thrown in uploadExternalProperties", ioe); return ResponseBuilder.notOk(Response.Status.INTERNAL_SERVER_ERROR, new FaultCodeException(FaultType.SERVICE_EXCEPTION, ioe.getMessage())); } catch (FileUploadException fue) { LOGGER.error("FileUploadException thrown in uploadExternalProperties", fue); return ResponseBuilder.notOk(Response.Status.INTERNAL_SERVER_ERROR, new FaultCodeException(FaultType.SERVICE_EXCEPTION, fue.getMessage())); } finally { assert data != null; try { data.close(); } catch (IOException e) { LOGGER.warn("IOException attempting to close external properties upload stream", e); } } return ResponseBuilder.ok(responseWrapper); } ResourceServiceRestImpl(final ResourceService resourceService); Response createTemplate(final List<Attachment> attachments, final String targetName, final AuthenticatedUser user); @Override Response getResourceAttrData(); @Override Response getResourceTopology(); @Override Response getApplicationResourceNames(final String groupName, final String appName); @Override Response getAppTemplate(final String groupName, final String appName, final String templateName); @Override Response checkFileExists(final String groupName, final String jvmName, final String webappName, final String webserverName, final String fileName); @Override Response createResource(final String deployFilename, final CreateResourceParam createResourceParam,
                                   final List<Attachment> attachments); void setMessageContext(MessageContext messageContext); @Override Response uploadExternalProperties(AuthenticatedUser user); @Override // TODO: Re validation, maybe we can use CXF bean validation ? Response deleteResource(final String templateName, final ResourceHierarchyParam resourceHierarchyParam,
                                   final AuthenticatedUser aUser); @Override Response deleteResources(final String[] templateNameArray, ResourceHierarchyParam
            resourceHierarchyParam, AuthenticatedUser user); @Override Response getResourceContent(final String resourceName, final ResourceHierarchyParam param); @Override Response updateResourceContent(String resourceName, ResourceHierarchyParam
            resourceHierarchyParam, String templateContent); @Override Response updateResourceMetaData(String resourceName, ResourceHierarchyParam resourceHierarchyParam, String metaData); @Override Response previewResourceContent(final String resourceName, final ResourceHierarchyParam resourceHierarchyParam, String content); @Override Response getResourcesFileNames(ResourceHierarchyParam resourceHierarchyParam); @Override Response getExternalProperties(); @Override Response getExternalPropertiesView(); @Override Response getExternalPropertiesDownload(); @Override void afterPropertiesSet(); }
ResourceServiceRestImpl implements ResourceServiceRest { @Override public Response uploadExternalProperties(AuthenticatedUser user) { LOGGER.info("Upload external properties by user {}", user.getUser().getId()); if (!context.getHttpHeaders().getAcceptableMediaTypes().contains(MediaType.APPLICATION_JSON_TYPE)) { context.getHttpServletResponse().setContentType(MediaType.TEXT_HTML); } ResourceIdentifier.Builder idBuilder = new ResourceIdentifier.Builder(); ResourceIdentifier resourceIdentifier = idBuilder.setResourceName(EXT_PROPERTIES_RESOURCE_NAME).build(); final ResourceTemplateMetaData metaData; InputStream data = null; CreateResourceResponseWrapper responseWrapper = null; try { metaData = resourceService.getMetaData(EXT_PROPERTIES_RESOURCE_META_DATA); ServletFileUpload sfu = new ServletFileUpload(); FileItemIterator iter = sfu.getItemIterator(context.getHttpServletRequest()); FileItemStream fileItemStream = iter.next(); data = fileItemStream.openStream(); responseWrapper = resourceService.createResource(resourceIdentifier, metaData, data); } catch (IOException ioe) { LOGGER.error("IOException thrown in uploadExternalProperties", ioe); return ResponseBuilder.notOk(Response.Status.INTERNAL_SERVER_ERROR, new FaultCodeException(FaultType.SERVICE_EXCEPTION, ioe.getMessage())); } catch (FileUploadException fue) { LOGGER.error("FileUploadException thrown in uploadExternalProperties", fue); return ResponseBuilder.notOk(Response.Status.INTERNAL_SERVER_ERROR, new FaultCodeException(FaultType.SERVICE_EXCEPTION, fue.getMessage())); } finally { assert data != null; try { data.close(); } catch (IOException e) { LOGGER.warn("IOException attempting to close external properties upload stream", e); } } return ResponseBuilder.ok(responseWrapper); } ResourceServiceRestImpl(final ResourceService resourceService); Response createTemplate(final List<Attachment> attachments, final String targetName, final AuthenticatedUser user); @Override Response getResourceAttrData(); @Override Response getResourceTopology(); @Override Response getApplicationResourceNames(final String groupName, final String appName); @Override Response getAppTemplate(final String groupName, final String appName, final String templateName); @Override Response checkFileExists(final String groupName, final String jvmName, final String webappName, final String webserverName, final String fileName); @Override Response createResource(final String deployFilename, final CreateResourceParam createResourceParam,
                                   final List<Attachment> attachments); void setMessageContext(MessageContext messageContext); @Override Response uploadExternalProperties(AuthenticatedUser user); @Override // TODO: Re validation, maybe we can use CXF bean validation ? Response deleteResource(final String templateName, final ResourceHierarchyParam resourceHierarchyParam,
                                   final AuthenticatedUser aUser); @Override Response deleteResources(final String[] templateNameArray, ResourceHierarchyParam
            resourceHierarchyParam, AuthenticatedUser user); @Override Response getResourceContent(final String resourceName, final ResourceHierarchyParam param); @Override Response updateResourceContent(String resourceName, ResourceHierarchyParam
            resourceHierarchyParam, String templateContent); @Override Response updateResourceMetaData(String resourceName, ResourceHierarchyParam resourceHierarchyParam, String metaData); @Override Response previewResourceContent(final String resourceName, final ResourceHierarchyParam resourceHierarchyParam, String content); @Override Response getResourcesFileNames(ResourceHierarchyParam resourceHierarchyParam); @Override Response getExternalProperties(); @Override Response getExternalPropertiesView(); @Override Response getExternalPropertiesDownload(); @Override void afterPropertiesSet(); static final String UNEXPECTED_CONTENT_TYPE_ERROR_MSG; static final String TPL_FILE_EXTENSION; static final String EXT_PROPERTIES_RESOURCE_NAME; static final String EXT_PROPERTIES_RESOURCE_META_DATA; static final int CREATE_RESOURCE_ATTACHMENT_SIZE; }