-
Notifications
You must be signed in to change notification settings - Fork 0
Import file API #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
omanikhi
wants to merge
14
commits into
master
Choose a base branch
from
import
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Import file API #14
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
9f2dd19
Draft of import options / parameters.
takesson 0a269f6
Defined import options for first implementation.
takesson 7ee0125
Implemented a transfer import command handler that imports a file fro…
omanikhi ad4e424
Implemented the TransformResource serving the REST functions.
omanikhi 970d3f1
Needed for Java 17 compatibility.
takesson 2d3b5a1
TODO: Consider supporting revision properties (backend recently suppo…
takesson f028cf1
Fixed the injection issues.
omanikhi 7633399
Fixed the JSON body deserialization issue.
omanikhi 8c22cf2
The importItem now supports folder items and auto-naming of the downl…
omanikhi cb1c916
Addressed the review comments.
omanikhi 45adf8a
Uncommented User-Agent and improved the itemId logging.
omanikhi c729f28
Incorporated pathnamebase and pathext parameters and implemented supp…
omanikhi 54de8a5
Make sure the item is fully qualified with a hostname in TransformRes…
omanikhi e3b22d3
A name pattern is required when the folder is a shard parent.
omanikhi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/main/java/se/simonsoft/cms/transform/command/TransformImportCommandHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /** | ||
| * Copyright (C) 2009-2017 Simonsoft Nordic AB | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package se.simonsoft.cms.transform.command; | ||
|
|
||
| import javax.inject.Inject; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import se.simonsoft.cms.item.CmsItemId; | ||
| import se.simonsoft.cms.item.CmsRepository; | ||
| import se.simonsoft.cms.item.command.ExternalCommandHandler; | ||
| import se.simonsoft.cms.transform.config.databind.TransformImportOptions; | ||
| import se.simonsoft.cms.transform.service.TransformService; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| public class TransformImportCommandHandler implements ExternalCommandHandler<TransformImportOptions> { | ||
|
|
||
| private final Logger logger = LoggerFactory.getLogger(TransformImportCommandHandler.class); | ||
| private final Map<CmsRepository, TransformService> transformServiceMap; | ||
|
|
||
| @Inject | ||
| public TransformImportCommandHandler(Map<CmsRepository, TransformService> transformServiceMap) { | ||
| this.transformServiceMap = transformServiceMap; | ||
| } | ||
|
|
||
| @Override | ||
| public String handleExternalCommand(CmsItemId item, TransformImportOptions arguments) { | ||
|
|
||
| logger.info("Starting import for item: {} from URL: {}", item.getLogicalId(), arguments.getUrl()); | ||
|
|
||
| try { | ||
| transformServiceMap.get(item.getRepository()).importItem(item, arguments); | ||
| String successMessage = "Import completed successfully for item: " + item.getLogicalId(); | ||
| logger.info(successMessage); | ||
| return successMessage; | ||
|
|
||
| } catch (Exception e) { | ||
| String errorMessage = "Import failed for item: " + item.getLogicalId() + " - " + e.getMessage(); | ||
| logger.error(errorMessage, e); | ||
| throw new RuntimeException(errorMessage, e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Class<TransformImportOptions> getArgumentsClass() { | ||
| return TransformImportOptions.class; | ||
| } | ||
| } |
92 changes: 92 additions & 0 deletions
92
src/main/java/se/simonsoft/cms/transform/config/databind/TransformImportOptions.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /** | ||
| * Copyright (C) 2009-2017 Simonsoft Nordic AB | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package se.simonsoft.cms.transform.config.databind; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
| import se.simonsoft.cms.item.properties.CmsItemPropertiesMap; | ||
|
|
||
| @JsonIgnoreProperties(ignoreUnknown = true) // Allow future changes. | ||
| public class TransformImportOptions { | ||
|
|
||
| // NOTE: This class is equivalent to TransformConfigOptions, but for import (external data) instead of transform (already committed). | ||
|
|
||
| /* | ||
| * The itemId is a separate parameter. | ||
| * | ||
| * Folder: | ||
| * - The itemId is the folder where the item should be imported. | ||
| * - The folder must be configured with auto-naming when importing non-XML. | ||
| * - The folder can be configured with auto-naming when importing XML (with or without XSL transform). | ||
| * - A folder with auto-naming will only allow a primary output from XSL transform. | ||
| * - A folder without auto-naming will not allow a primary output from XSL transform. | ||
| * | ||
| * File: | ||
| * - The imported file will become that itemId (param 'overwrite' must be true if item already exists). | ||
| * - TBD: Will additional output be allowed from XSL transform? | ||
| */ | ||
|
|
||
| /* | ||
| * Params: | ||
| * - 'comment': History comment for the commit. | ||
| * - 'overwrite': Must be true in order to allow overwriting an existing item in CMS. | ||
| * | ||
| * - Future: 'TransformNN' | ||
| */ | ||
| private Map <String, String> params = new HashMap<>(); | ||
| private String url; // Typically an http / https url, no authentication required. Redirects must be followed. | ||
| private String content; // Content to import, typically XML or JSON. | ||
| private Map <String, String> properties = new HashMap<>(); // Properties to set on the item. | ||
| private Map <String, String> revprops = new HashMap<>(); // TODO: Consider supporting revision properties (backend recently supports). | ||
|
|
||
|
|
||
| public Map<String, String> getParams() { | ||
| return params; | ||
| } | ||
| public void setParams(Map<String, String> params) { | ||
| this.params = params; | ||
| } | ||
| public String getUrl() { | ||
| return url; | ||
| } | ||
| public void setUrl(String url) { | ||
| this.url = url; | ||
| } | ||
| public String getContent() { | ||
| return content; | ||
| } | ||
| public void setContent(String content) { | ||
| this.content = content; | ||
| } | ||
| public Map<String, String> getProperties() { | ||
| return properties; | ||
| } | ||
| public void setProperties(Map<String, String> properties) { | ||
| this.properties = properties; | ||
| } | ||
|
|
||
| public CmsItemPropertiesMap getItemPropertiesMap() { | ||
| CmsItemPropertiesMap m = new CmsItemPropertiesMap(); | ||
| if (getProperties() != null) { | ||
| getProperties().forEach((key, value) -> { | ||
| m.put(key, value); | ||
| }); | ||
| } | ||
| return m; | ||
| } | ||
| } |
114 changes: 114 additions & 0 deletions
114
src/main/java/se/simonsoft/cms/transform/rest/TransformResource.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| /** | ||
| * Copyright (C) 2009-2017 Simonsoft Nordic AB | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package se.simonsoft.cms.transform.rest; | ||
|
|
||
| import javax.inject.Inject; | ||
| import javax.inject.Named; | ||
| import javax.ws.rs.Consumes; | ||
| import javax.ws.rs.POST; | ||
| import javax.ws.rs.Path; | ||
| import javax.ws.rs.Produces; | ||
| import javax.ws.rs.QueryParam; | ||
| import javax.ws.rs.core.MediaType; | ||
| import javax.ws.rs.core.Response; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.databind.ObjectWriter; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import se.simonsoft.cms.item.CmsItemId; | ||
| import se.simonsoft.cms.item.CmsRepository; | ||
| import se.simonsoft.cms.item.impl.CmsItemIdArg; | ||
| import se.simonsoft.cms.transform.service.TransformService; | ||
| import se.simonsoft.cms.transform.config.databind.TransformImportOptions; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| @Path("/transform5") | ||
| public class TransformResource { | ||
|
|
||
| private final Logger logger = LoggerFactory.getLogger(TransformResource.class); | ||
| private final Map<CmsRepository, TransformService> transformServiceMap; | ||
| private final ObjectWriter objectWriter; | ||
| private String hostname; | ||
|
|
||
| private static final int MAX_CONTENT_SIZE_MB = 5; | ||
|
|
||
| @Inject | ||
| public TransformResource( | ||
| @Named("config:se.simonsoft.cms.hostname") String hostname, | ||
| Map<CmsRepository, TransformService> transformServiceMap, | ||
| ObjectWriter objectWriter) { | ||
| this.hostname = hostname; | ||
| this.transformServiceMap = transformServiceMap; | ||
| this.objectWriter = objectWriter; | ||
| } | ||
|
|
||
| @POST | ||
| @Path("api/import") | ||
| @Consumes(MediaType.APPLICATION_JSON) | ||
| @Produces(MediaType.APPLICATION_JSON) | ||
| public Response importItem(@QueryParam("item") CmsItemIdArg item, String body) throws JsonProcessingException { | ||
|
|
||
| if (item == null) { | ||
| throw new IllegalArgumentException("Field 'item': required"); | ||
| } | ||
|
|
||
| item.setHostnameOrValidate(hostname); | ||
|
|
||
| TransformImportOptions importOptions; | ||
|
|
||
| try { | ||
| ObjectMapper mapper = new ObjectMapper(); | ||
| importOptions = mapper.readValue(body, TransformImportOptions.class); | ||
| } catch (JsonProcessingException e) { | ||
| logger.error("API request with invalid JSON body: {}", body, e); | ||
| throw new IllegalArgumentException("Failed to parse request body: " + e.getMessage(), e); | ||
| } | ||
|
|
||
| if (importOptions == null) { | ||
| return Response.status(Response.Status.BAD_REQUEST) | ||
| .entity("{\"error\": \"Import options must be provided in request body\"}") | ||
| .build(); | ||
| } | ||
|
|
||
| String url = importOptions.getUrl(); | ||
| String content = importOptions.getContent(); | ||
|
|
||
| if ((content != null && url != null) || (content == null && url == null)) { | ||
| throw new IllegalArgumentException("Import requires either a valid URL or content."); | ||
| } else if (content != null && content.length() > MAX_CONTENT_SIZE_MB * 1024 * 1024) { | ||
| throw new IllegalArgumentException(String.format("Largest allowed content size is %d MBs.", MAX_CONTENT_SIZE_MB)); | ||
| } | ||
|
|
||
| try { | ||
| Map<String, Set<CmsItemId>> response = new HashMap<>(); | ||
| Set<CmsItemId> items = transformServiceMap.get(item.getRepository()).importItem(item, importOptions); | ||
| response.put("items", items); | ||
| return Response.ok().entity(objectWriter.writeValueAsString(response)).build(); | ||
| } catch (IllegalArgumentException e) { | ||
| logger.warn("Invalid input parameters for item: {}, importOptions: {}", item, body, e); | ||
| throw e; | ||
| } catch (Exception e) { | ||
| logger.error("Import failed for item: {}, error: {}", item, e.getMessage()); | ||
| throw e; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.