Skip to content

Commit

Permalink
[FIXED JENKINS-17031] add new provider for Json files
Browse files Browse the repository at this point in the history
  • Loading branch information
imod committed Mar 10, 2013
1 parent 881e698 commit 046add3
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 1 deletion.
102 changes: 102 additions & 0 deletions src/main/java/org/jenkinsci/plugins/configfiles/json/JsonConfig.java
@@ -0,0 +1,102 @@
/*
The MIT License
Copyright (c) 2012, Dominik Bartholdi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package org.jenkinsci.plugins.configfiles.json;

import hudson.Extension;

import org.jenkinsci.lib.configprovider.AbstractConfigProviderImpl;
import org.jenkinsci.lib.configprovider.model.Config;
import org.jenkinsci.lib.configprovider.model.ContentType;
import org.jenkinsci.plugins.configfiles.Messages;
import org.kohsuke.stapler.DataBoundConstructor;

/**
* A config/provider to handle the special case of a Json file
*
* @author Dominik Bartholdi (imod)
*
*/
public class JsonConfig extends Config {
private static final long serialVersionUID = 1L;

@DataBoundConstructor
public JsonConfig(String id, String name, String comment, String content) {
super(id, name, comment, fixJsonContent(content));
}

/**
* as the form submission with stapler is done in json too, we have to do "deescape" the formated content of the json file.
*
* @param content
* the json body of the file
* @return deescaped json
*/
private static String fixJsonContent(String content) {
final String c = content.trim();
if (c.startsWith("\"") && c.endsWith("\"")) {
return c.substring(1, c.length() - 1);
}
return c;
}

@Extension(ordinal = 180)
public static class JsonConfigProvider extends AbstractConfigProviderImpl {

public JsonConfigProvider() {
load();
}

@Override
public ContentType getContentType() {
return new ContentType() {
public String getCmMode() {
return "javascript";
}

public String getMime() {
return "application/json";
}
};
}

@Override
public String getDisplayName() {
return Messages.json_provider_name();
}

@Override
public JsonConfig getConfigById(String configId) {
final Config c = super.getConfigById(configId);
return new JsonConfig(c.id, c.name, c.comment, c.content);
}

@Override
public Config newConfig() {
String id = getProviderId() + System.currentTimeMillis();
return new Config(id, "JsonConfig", "", "{}");
}

}

}
Expand Up @@ -31,5 +31,5 @@ xml_provider_name=Simple XML file
mvn_global_settings_provider_name=Global Maven settings.xml
mvn_settings_provider_name=Maven settings.xml
mvn_toolchains_provider_name=Maven toolchains.xml

json_provider_name=Json file

@@ -0,0 +1,29 @@
<!--
The MIT License
Copyright (c) 2013, Dominik Bartholdi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->



<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
${%json_provider_description}
</j:jelly>
@@ -0,0 +1,24 @@
# The MIT License
#
# Copyright (c) 2013, Dominik Bartholdi
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

json_provider_description=a Json file

@@ -0,0 +1,43 @@
<!--
The MIT License
Copyright (c) 2013, Dominik Bartholdi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->



<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define"
xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">

<f:entry title="${%ID}" >
<f:textbox readonly="readonly" name="config.id" value="${config.id}" />
</f:entry>
<f:entry title="${%Name}">
<f:textbox name="config.name" value="${config.name}" />
</f:entry>
<f:entry title="${%Comment}">
<f:textbox name="config.comment" value="${config.comment}" />
</f:entry>
<f:entry title="${%Content}">
<f:textarea id="config.content" name="config.content" value="${config.content}" />
</f:entry>

</j:jelly>

0 comments on commit 046add3

Please sign in to comment.