Search in sources :

Example 21 with JSONException

use of net.sf.json.JSONException in project blueocean-plugin by jenkinsci.

the class GitScm method validateAndCreate.

@Override
public HttpResponse validateAndCreate(@JsonBody JSONObject request) {
    boolean requirePush = request.has("requirePush");
    final String repositoryUrl;
    final AbstractGitSCMSource scmSource;
    if (request.has("repositoryUrl")) {
        scmSource = null;
        repositoryUrl = request.getString("repositoryUrl");
    } else {
        try {
            String fullName = request.getJSONObject("pipeline").getString("fullName");
            SCMSourceOwner item = Jenkins.getInstance().getItemByFullName(fullName, SCMSourceOwner.class);
            if (item != null) {
                scmSource = (AbstractGitSCMSource) item.getSCMSources().iterator().next();
                repositoryUrl = scmSource.getRemote();
            } else {
                return HttpResponses.errorJSON("No repository found for: " + fullName);
            }
        } catch (JSONException e) {
            return HttpResponses.errorJSON("No repositoryUrl or pipeline.fullName specified in request.");
        } catch (RuntimeException e) {
            return HttpResponses.errorWithoutStack(ServiceException.INTERNAL_SERVER_ERROR, e.getMessage());
        }
    }
    try {
        String credentialId = request.getString("credentialId");
        User user = User.current();
        if (user == null) {
            throw new ServiceException.UnauthorizedException("Not authenticated");
        }
        final StandardCredentials creds = CredentialsMatchers.firstOrNull(CredentialsProvider.lookupCredentials(StandardCredentials.class, Jenkins.getInstance(), Jenkins.getAuthentication(), (List<DomainRequirement>) null), CredentialsMatchers.allOf(CredentialsMatchers.withId(credentialId)));
        if (creds == null) {
            throw new ServiceException.NotFoundException("No credentials found for: " + credentialId);
        }
        if (requirePush) {
            String branch = request.getString("branch");
            new GitBareRepoReadSaveRequest(scmSource, branch, null, branch, null, null).invokeOnScm(new GitSCMFileSystem.FSFunction<Void>() {

                @Override
                public Void invoke(Repository repository) throws IOException, InterruptedException {
                    GitUtils.validatePushAccess(repository, repositoryUrl, creds);
                    return null;
                }
            });
        } else {
            List<ErrorMessage.Error> errors = GitUtils.validateCredentials(repositoryUrl, creds);
            if (!errors.isEmpty()) {
                throw new ServiceException.UnauthorizedException(errors.get(0).getMessage());
            }
        }
    } catch (Exception e) {
        return HttpResponses.errorWithoutStack(ServiceException.PRECONDITION_REQUIRED, e.getMessage());
    }
    return HttpResponses.okJSON();
}
Also used : User(hudson.model.User) SCMSourceOwner(jenkins.scm.api.SCMSourceOwner) GitSCMFileSystem(jenkins.plugins.git.GitSCMFileSystem) JSONException(net.sf.json.JSONException) IOException(java.io.IOException) JSONException(net.sf.json.JSONException) IOException(java.io.IOException) ServiceException(io.jenkins.blueocean.commons.ServiceException) Repository(org.eclipse.jgit.lib.Repository) AbstractGitSCMSource(jenkins.plugins.git.AbstractGitSCMSource) List(java.util.List) StandardCredentials(com.cloudbees.plugins.credentials.common.StandardCredentials)

Example 22 with JSONException

use of net.sf.json.JSONException in project jmeter-plugins by undera.

the class JSONFormatter method process.

@Override
public void process() {
    JMeterContext context = getThreadContext();
    String responseData = context.getPreviousResult().getResponseDataAsString();
    try {
        String str = this.formatJSON(responseData);
        context.getPreviousResult().setResponseData(str.getBytes());
    } catch (JSONException e) {
        log.warn("Failed to format JSON: " + e.getMessage());
        log.debug("Failed to format JSON", e);
    }
}
Also used : JMeterContext(org.apache.jmeter.threads.JMeterContext) JSONException(net.sf.json.JSONException)

Example 23 with JSONException

use of net.sf.json.JSONException in project phabricator-jenkins-plugin by uber.

the class DifferentialClient method fetchDiff.

/**
 * Fetch a differential from Conduit
 * @return the Conduit API response
 * @throws IOException if there is a network error talking to Conduit
 * @throws ConduitAPIException if any error is experienced talking to Conduit
 */
public JSONObject fetchDiff() throws IOException, ConduitAPIException {
    JSONObject params = new JSONObject().element("ids", new String[] { diffID });
    JSONObject query = this.callConduit("differential.querydiffs", params);
    JSONObject response;
    try {
        response = query.getJSONObject("result");
    } catch (JSONException e) {
        throw new ConduitAPIException(String.format("No 'result' object found in conduit call: (%s) %s", e.getMessage(), query.toString(2)));
    }
    try {
        return response.getJSONObject(diffID);
    } catch (JSONException e) {
        throw new ConduitAPIException(String.format("Unable to find '%s' key in 'result': (%s) %s", diffID, e.getMessage(), response.toString(2)));
    }
}
Also used : JSONObject(net.sf.json.JSONObject) JSONException(net.sf.json.JSONException)

Example 24 with JSONException

use of net.sf.json.JSONException in project pmph by BCSquad.

the class WechatAccessToken method getAccessToken.

// https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRECT
/**
 * <pre>
 * 功能描述:获取access_token
 * 使用示范:
 *
 * @param appid 凭证
 * @param appsecret 密钥
 * @param type
 * @return
 * </pre>
 */
public static AccessToken getAccessToken(String appid, String appsecret, int type) {
    AccessToken accessToken = null;
    String requestUrl = access_token_url.replace("APPID", appid).replace("APPSECRET", appsecret);
    if (type == 1) {
        requestUrl = company_access_token_url.replace("CORPID", appid).replace("CORPSECRET", appsecret);
    // System.err.println(requestUrl);
    }
    JSONObject jsonObject = HttpRequestUtil.httpRequest(requestUrl, EnumMethod.GET.name(), null);
    if (jsonObject == null) {
        jsonObject = HttpRequestUtil.httpRequest(requestUrl, EnumMethod.GET.name(), null);
    }
    // 如果请求成功
    if (null != jsonObject) {
        try {
            accessToken = new AccessToken();
            accessToken.setToken(jsonObject.getString("access_token"));
            accessToken.setExpiresIn(jsonObject.getInt("expires_in"));
        } catch (JSONException e) {
            accessToken = null;
        // 获取token失败
        }
    }
    return accessToken;
}
Also used : JSONObject(net.sf.json.JSONObject) AccessToken(com.bc.pmpheep.wechat.po.AccessToken) JSONException(net.sf.json.JSONException)

Aggregations

JSONException (net.sf.json.JSONException)24 JSONObject (net.sf.json.JSONObject)13 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 PatternSyntaxException (java.util.regex.PatternSyntaxException)4 DatabaseException (org.parosproxy.paros.db.DatabaseException)4 ApiException (org.zaproxy.zap.extension.api.ApiException)4 Context (org.zaproxy.zap.model.Context)4 JSONArray (net.sf.json.JSONArray)3 CamelExecutionException (org.apache.camel.CamelExecutionException)3 Exchange (org.apache.camel.Exchange)3 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)3 URIException (org.apache.commons.httpclient.URIException)3 Test (org.junit.Test)3 Session (org.parosproxy.paros.model.Session)3 ApiResponseElement (org.zaproxy.zap.extension.api.ApiResponseElement)3 User (org.zaproxy.zap.users.User)3 BufferedInputStream (java.io.BufferedInputStream)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2