public boolean putPage(String wikiName, String spaceName, String pageName, InputStream pageXML) { HttpClient httpClient = restResource.getClient(); String uri = String.format( "%s/wikis/%s/spaces/%s/pages/%s", DefaultRestResource.REST_URI, wikiName, spaceName, pageName); PutMethod putMethod = new PutMethod(uri); putMethod.addRequestHeader("Accept", "application/xml"); putMethod.addRequestHeader("Accept-Ranges", "bytes"); RequestEntity pageRequestEntity = new InputStreamRequestEntity(pageXML, "application/xml"); putMethod.setRequestEntity(pageRequestEntity); try { httpClient.executeMethod(putMethod); return true; } catch (HttpException e) { String message = String.format( "Unable to process the PUT request on page '%s:%s.%s'.", wikiName, spaceName, pageName); logger.error(message, e); return false; } catch (IOException e) { String message = String.format("Unable to PUT the page '%s:%s.%s'.", wikiName, spaceName, pageName); logger.error(message, e); return false; } }
public boolean isPage(String wikiName, String spaceName, String pageName) { HttpClient httpClient = restResource.getClient(); String uri = String.format( "%s/wikis/%s/spaces/%s/pages/%s", DefaultRestResource.REST_URI, wikiName, spaceName, pageName); GetMethod getMethod = new GetMethod(uri); getMethod.addRequestHeader("Accept", "application/xml"); getMethod.addRequestHeader("Accept-Ranges", "bytes"); try { int statusCode = httpClient.executeMethod(getMethod); if (statusCode == 200) { return true; } } catch (HttpException e) { String message = String.format( "Unable to process the GET request on page '%s:%s.%s'.", wikiName, spaceName, pageName); logger.error(message, e); return false; } catch (IOException e) { String message = String.format("Unable to GET the page '%s:%s.%s'.", wikiName, spaceName, pageName); logger.error(message, e); return false; } return false; }
public InputStream getAttachment( String wikiName, String spaceName, String pageName, String attachmentName) { HttpClient httpClient = restResource.getClient(); String uri = String.format( "%s/wikis/%s/spaces/%s/pages/%s/attachments/%s", DefaultRestResource.REST_URI, wikiName, spaceName, pageName, attachmentName); GetMethod getMethod = new GetMethod(uri); getMethod.addRequestHeader("Accept", "application/xml"); getMethod.addRequestHeader("Accept-Ranges", "bytes"); try { httpClient.executeMethod(getMethod); return getMethod.getResponseBodyAsStream(); } catch (HttpException e) { String message = String.format( "Unable to process GET request for the attachment '%s:%s.%s@%s'.", wikiName, spaceName, pageName, attachmentName); logger.error(message, e); return null; } catch (IOException e) { String message = String.format( "Unable to GET the attachment '%s:%s.%s@%s'.", wikiName, spaceName, pageName, attachmentName); logger.error(message, e); return null; } }
@Test public void shouldListenToChangesInConfiguredParameters() throws Exception { RestService parentService = restResource.getService(); RestResourceConfig config = RestResourceConfig.Factory.newInstance(); RestParametersConfig restParametersConfig = config.addNewParameters(); RestParameterConfig parameterConfig = restParametersConfig.addNewParameter(); String parameterName = "theName"; parameterConfig.setName(parameterName); parameterConfig.setStyle( RestParameterConfig.Style.Enum.forInt( RestParamsPropertyHolder.ParameterStyle.QUERY.ordinal())); config.setPath("/actual_path"); RestResource restResource = new RestResource(parentService, config); restResource .getParams() .getProperty(parameterName) .setStyle(RestParamsPropertyHolder.ParameterStyle.TEMPLATE); assertThat(restResource.getPath(), containsString(parameterName)); }
@Test public void shouldGetTemplateParams() throws Exception { assertThat(restResource.getDefaultParams(), is(anEmptyArray())); restResource.setPath("/{id}/test"); assertThat(restResource.getDefaultParams(), is(anEmptyArray())); assertThat(restResource.getFullPath(), is("/{id}/test")); RestResource subResource = restResource.addNewChildResource("Child", "{test}/test"); assertThat(subResource.getFullPath(), is("/{id}/test/{test}/test")); }
public RestParamProperty[] getDefaultParams() { List<RestParamProperty> result = new ArrayList<RestParamProperty>(); Set<String> names = new HashSet<String>(); result.addAll(Arrays.asList(resource.getDefaultParams())); for (int c = 0; c < getPropertyCount(); c++) { if (names.contains(getPropertyAt(c).getName())) continue; result.add(getPropertyAt(c)); names.add(getPropertyAt(c).getName()); } return result.toArray(new RestParamProperty[result.size()]); }
@Test public void shouldIgnoreMatrixParamsWithoutValueOnPath() throws Exception { String matrixParameterString = ";Param2=1;address="; restResource.setPath("/maps/api/geocode/xml" + matrixParameterString); assertThat(restResource.getFullPath(), not(containsString(matrixParameterString))); String childResourceParameterString = ";ver="; RestResource childResource = restResource.addNewChildResource( "Child", "{test}/test/version" + childResourceParameterString); assertThat(childResource.getPath(), not(containsString(childResourceParameterString))); assertThat(childResource.getFullPath(), not(containsString(matrixParameterString))); assertThat(childResource.getFullPath(), not(containsString(childResourceParameterString))); }
public String getEmailAddress(String wikiName, String username) throws LpRestExceptionXWikiImpl { String emailAddress = null; HttpClient httpClient = restResource.getClient(); // http://<server>/rest/wikis/xwiki/spaces/XWiki/pages/<username>/objects/XWiki.XWikiUsers/0/properties/email String uri = String.format( "%s/wikis/%s/spaces/XWiki/pages/%s/objects/XWiki.XWikiUsers/0/properties/email", DefaultRestResource.REST_URI, wikiName, username); GetMethod getMethod = new GetMethod(uri); try { httpClient.executeMethod(getMethod); InputStream response = getMethod.getResponseBodyAsStream(); JAXBContext jaxbContext = JAXBContext.newInstance(Property.class); Property emailProperty = (Property) jaxbContext.createUnmarshaller().unmarshal(response); emailAddress = emailProperty.getValue(); } catch (IOException e) { String message = String.format( "Unable to GET the email propery of '%s' on %s@%s'.", username, wikiName, DefaultRestResource.REST_URI); logger.error(message, e); throw new LpRestExceptionXWikiImpl(message, e.getCause()); } catch (JAXBException e) { String message = String.format( "Unable to unmarshall the email propery of '%s' on %s@%s'.", username, wikiName, DefaultRestResource.REST_URI); logger.error(message, e); throw new LpRestExceptionXWikiImpl(message, e.getCause()); } return emailAddress; }
private RestParamsPropertyHolder buildOverlay(RestResource resource) { return resource.getParentResource() == null ? resource.getParams() : new OverlayRestParamsPropertyHolder( buildOverlay(resource.getParentResource()), resource.getParams()); }
public RestService getInterface() { return resource.getInterface(); }