public DetailedLink build() { if (!link.getRequest().isSetBody()) { link.getRequest().setBody(new org.ovirt.engine.api.model.Body()); } return addParametersMetadata(link); } }
/** * Create an instance of {@link DetailedLink } * */ public DetailedLink createDetailedLink() { return new DetailedLink(); }
public LinkBuilder httpMethod(HttpMethod httpMethod) { if(!link.isSetRequest()) { link.setRequest(new Request()); } link.getRequest().setHttpMethod(httpMethod); return this; } public DetailedLink build() {
private Api getApi() { Api api = new Api(); api.setTime(DateMapper.map(new Date(), null)); for (DetailedLink detailedLink : getLinks()) { //add thin link api.getLinks().add(LinkCreator.createLink(detailedLink.getHref(), detailedLink.getRel())); //when required - add extra link for search if (detailedLink.isSetLinkCapabilities() && detailedLink.getLinkCapabilities().isSetSearchable() && detailedLink.getLinkCapabilities().isSearchable()) { api.getLinks().add(LinkCreator.createLink(detailedLink.getHref(), detailedLink.getRel(), detailedLink.getRequest().getUrl().getParametersSets())); } //add special links SpecialObjects specialObjects = new SpecialObjects(); specialObjects.setBlankTemplate(createBlankTemplate()); specialObjects.setRootTag(createRootTag()); api.setSpecialObjects(specialObjects); } return api; }
/** * Create a search link with the given parameters * * @param url the url * @param rel the link to add * @param flags flags for this link, e.g: 'searchable' * @param params url parameters * @return the link the was created */ public static DetailedLink createLink(String url, String rel, LinkFlags flags, ParametersSet params) { DetailedLink link = new DetailedLink(); link.setRel(rel); link.setHref(combine(url, rel)); if (flags == LinkFlags.SEARCHABLE) { LinkCapabilities capabilities = new LinkCapabilities(); capabilities.setSearchable(true); link.setLinkCapabilities(capabilities); } link.setRequest(new Request()); link.getRequest().setUrl(new Url()); link.getRequest().getUrl().getParametersSets().add(params); return link; }
private DetailedLink addParametersMetadata(DetailedLink link) { String link_name = link.getHref() + "|rel=" + link.getRel(); if (this.parametersMetaData.containsKey(link_name)) { Action action = this.parametersMetaData.get(link_name); if (action.getDescription() != null) { link.setDescription(action.getDescription()); } if (action.getRequest() != null) { addUrlParams(link, action); addHeaderParams(link, action); addBodyParams(link, action); } } return link; }
/** * There is a special kind of url: a url that may receive a body (with parameters in it), * or may not. For example, when deleting a datacenter, the user may pass nothing in the body, * or may pass <action><force>true</force></action>. * * RSDL builder will encounter both signatures during construction, and when it encounters the * first is has no knowledge of the second yet, so it must create both linke. * * This method will be called at the end of construction, and search for such duplicate links. * It will unite these pairs into a single link with required=false in the <body>. */ private void uniteDuplicateLinks(Rsdl rsdl) { Map<String, DetailedLink> linksMap = new HashMap<>(); Collection<DetailedLink> linksToDelete = new LinkedList<>(); for (DetailedLink link : rsdl.getLinks().getLinks()) { String linkId = link.getHref() + link.getRel(); if (linksMap.containsKey(linkId)) { //duplicate found, determine which of the two should be deleted DetailedLink linkToDelete = decideWhichToDelete(linksMap.get(linkId), link); if (linkToDelete!=null) { linksToDelete.add(linkToDelete); } } else { linksMap.put(linkId, link); } } for (DetailedLink link : linksToDelete) { rsdl.getLinks().getLinks().remove(link); } }
private void addHeaderParams(DetailedLink link, Action action) { link.getRequest().setHeaders(new Headers()); for (Object key : action.getRequest().getHeaders().keySet()) { Header header = new Header(); link.getRequest().getHeaders().getHeaders().add(header); if (!GET.equals(link.getRel())) { addCorrelationIdHeader(link); if (!GET.equals(link.getRel())) { addContentTypeHeader(link); if (ADD.equals(link.getRel())) { addExpectHeader(link, "201-created"); if (UPDATE.equals(link.getRel())) { addExpectHeader(link, "202-accepted");
/** * Adds to a link the a URL parameter, creating all the intermediate objects if they don't exist. * * @param link the link where the parameters will be added * @param parameter the parameter to add */ private void addUrlParameter(DetailedLink link, Parameter parameter) { Request request = link.getRequest(); if (request == null) { request = new Request(); link.setRequest(request); } Url url = request.getUrl(); if (url == null) { url = new Url(); request.setUrl(url); } List<ParametersSet> parametersSets = url.getParametersSets(); ParametersSet parametersSet; if (parametersSets.isEmpty()) { parametersSet = new ParametersSet(); parametersSets.add(parametersSet); } else { parametersSet = parametersSets.get(0); } parametersSet.getParameters().add(parameter); }
public LinkBuilder url(String url) { link.setHref(url); return this; } public LinkBuilder description(String description) {
public LinkBuilder description(String description) { link.setDescription(description); return this; } public LinkBuilder rel(String rel) {
private Api getGlusterApi() { Api api = new Api(); api.setTime(DateMapper.map(new Date(), null)); for (DetailedLink detailedLink : getGlusterLinks()) { // add thin link api.getLinks().add(LinkCreator.createLink(detailedLink.getHref(), detailedLink.getRel())); // when required - add extra link for search if (detailedLink.isSetLinkCapabilities() && detailedLink.getLinkCapabilities().isSetSearchable() && detailedLink.getLinkCapabilities().isSearchable()) { api.getLinks().add(LinkCreator.createLink(detailedLink.getHref(), detailedLink.getRel(), detailedLink.getRequest().getUrl().getParametersSets())); } // add special links SpecialObjects specialObjects = new SpecialObjects(); specialObjects.setRootTag(createRootTag()); api.setSpecialObjects(specialObjects); } return api; }
private DetailedLink decideWhichToDelete(DetailedLink link1, DetailedLink link2) { String link1ParamType = link1.getRequest().getBody().getType(); String link2ParamType = link2.getRequest().getBody().getType(); //Verify for both links that body is not mandatory if ( ( (link1.getRequest().getBody().isRequired() != null) && (link2.getRequest().getBody().isRequired() != null) ) && ( (Boolean.FALSE.equals(link1.getRequest().getBody().isRequired())) && (Boolean.FALSE.equals(link2.getRequest().getBody().isRequired())) ) ) { if (link1ParamType!=null && link2ParamType==null) { return link2; } if (link1ParamType==null && link2ParamType!=null) { return link1; } } return null; } }
/** * Adds the description of the {@code Content-Type} header to a link. * * @param link the link where the description of the header will be added */ private void addContentTypeHeader(DetailedLink link) { Headers headers = link.getRequest().getHeaders(); if (headers == null) { headers = new Headers(); link.getRequest().setHeaders(headers); } Header header = new Header(); header.setName("Content-Type"); header.setValue("application/xml|json"); header.setRequired(true); headers.getHeaders().add(header); }
/** * Adds the description of the {@code Expect} header to a link. * * @param link the link where the description of the header will be added * @param value the value of the header */ private void addExpectHeader(DetailedLink link, String value) { Headers headers = link.getRequest().getHeaders(); if (headers == null) { headers = new Headers(); link.getRequest().setHeaders(headers); } Header header = new Header(); header.setName("Expect"); header.setValue(value); header.setRequired(false); headers.getHeaders().add(header); }
/** * Adds the description of the {@code Correlation-Id} header to a link. * * @param link the link where the description of the header will be added */ private void addCorrelationIdHeader(DetailedLink link) { Headers headers = link.getRequest().getHeaders(); if (headers == null) { headers = new Headers(); link.getRequest().setHeaders(headers); } Header header = new Header(); header.setName("Correlation-Id"); header.setValue("any string"); header.setRequired(false); headers.getHeaders().add(header); }
/** * Adds to a link the parameters that are common to all actions, like {@code async} and {@code grace_period.expiry}. * These parameters will be added to all the signatures that have a body of type {@code Action}. * * @param link the link where the parameters will be added */ private void addCommonActionParameters(DetailedLink link) { Request request = link.getRequest(); if (request != null) { org.ovirt.engine.api.model.Body body = request.getBody(); if (body != null) { String type = body.getType(); if (ACTION.equals(type)) { List<ParametersSet> parametersSets = body.getParametersSets(); for (ParametersSet parametersSet : parametersSets) { List<Parameter> parameters = parametersSet.getParameters(); parameters.add(newGracePeriodParameter()); parameters.add(newAsyncActionParameter()); } } } } }
private void handleDelete(String prefix, Collection<DetailedLink> results, Method m) { DetailedLink link = new RsdlBuilder.LinkBuilder().url(prefix) .rel(DELETE) .httpMethod(HttpMethod.DELETE) .build(); Class<?>[] parameterTypes = m.getParameterTypes(); if (parameterTypes.length > 0) { link.getRequest().getBody().setType(parameterTypes[0].getSimpleName()); } addCommonActionParameters(link); addAsyncMatrixParameter(link); results.add(link); }
private void addUrlParams(DetailedLink link, Action action) { if (action.getRequest().getUrlparams() != null && !action.getRequest().getUrlparams().isEmpty()) { link.getRequest().setUrl(new Url()); ParametersSet ps = new ParametersSet(); for (Object key : action.getRequest().getUrlparams().keySet()) { Parameter param = new Parameter(); param.setName(key.toString()); Object value = action.getRequest().getUrlparams().get(key); if (value != null) { ParamData urlParamData = (ParamData)value; param.setType(urlParamData.getType()); param.setContext(urlParamData.getContext()); param.setValue(urlParamData.getValue()); param.setRequired(urlParamData.getRequired()==null ? Boolean.FALSE : urlParamData.getRequired()); param.setDeprecated(urlParamData.getDeprecated()); } ps.getParameters().add(param); } link.getRequest().getUrl().getParametersSets().add(ps); } }
private void addBodyParams(DetailedLink link, Action action) { if (action.getRequest().getBody() != null) { link.getRequest().getBody().setRequired(action.getRequest().getBody().isRequired()); if (action.getRequest().getBody().getSignatures() != null) { for (Signature signature : action.getRequest().getBody().getSignatures()) { ParametersSet ps = new ParametersSet(); if (signature.getDeprecated() != null) { ps.setDeprecated(signature.getDeprecated()); } if (signature.getDescription() != null) { ps.setDescription(signature.getDescription()); } addBodyParams(ps, signature.getMandatoryArguments().entrySet(), true); addBodyParams(ps, signature.getOptionalArguments().entrySet(), false); link.getRequest().getBody().getParametersSets().add(ps); } } } }