private OrcidMessage fakeBio() throws DatatypeConfigurationException {
    OrcidMessage orcidMessage = new OrcidMessage();
    OrcidProfile orcidProfile1 = new OrcidProfile();
    OrcidIdentifier orcidIdentifier = new OrcidIdentifier();
    orcidProfile1.setOrcidIdentifier(orcidIdentifier);
    orcidIdentifier.setUri("http://orcid.example.com/000-1337");
    orcidIdentifier.setPath("000-1337");
    OrcidBio bio = new OrcidBio();
    orcidProfile1.setOrcidBio(bio);
    OrcidHistory history = new OrcidHistory();
    XMLGregorianCalendar value =
        dataTypeFactory.newXMLGregorianCalendar(1980, 12, 31, 23, 29, 29, 999, 0);
    history.setCreationMethod(CreationMethod.WEBSITE);
    history.setLastModifiedDate(new LastModifiedDate(value));
    orcidProfile1.setOrcidHistory(history);
    PersonalDetails personal = new PersonalDetails();
    bio.setPersonalDetails(personal);
    personal.setFamilyName(new FamilyName("Doe"));
    personal.setCreditName(new CreditName("John F Doe"));
    personal.setGivenNames(new GivenNames("John"));
    personal.setOtherNames(new OtherNames());
    personal.getOtherNames().addOtherName("Johnny", Visibility.PUBLIC);
    personal.getOtherNames().addOtherName("Mr Doe", Visibility.PUBLIC);

    ResearcherUrls urls = new ResearcherUrls();
    bio.setResearcherUrls(urls);

    ResearcherUrl anonymous =
        new ResearcherUrl(new Url("http://example.com/anon"), Visibility.PUBLIC);
    urls.getResearcherUrl().add(anonymous);

    // "home page" - with strange casing
    ResearcherUrl homePage =
        new ResearcherUrl(
            new Url("http://example.com/myPage"), new UrlName("homePage"), Visibility.PUBLIC);
    urls.getResearcherUrl().add(homePage);

    ResearcherUrl foaf =
        new ResearcherUrl(
            new Url("http://example.com/foaf#me"), new UrlName("FOAF"), Visibility.PUBLIC);
    urls.getResearcherUrl().add(foaf);

    ResearcherUrl webId =
        new ResearcherUrl(
            new Url("http://example.com/webId"), new UrlName("webID"), Visibility.PUBLIC);
    urls.getResearcherUrl().add(webId);

    ResearcherUrl other =
        new ResearcherUrl(
            new Url("http://example.com/other"), new UrlName("other"), Visibility.PUBLIC);
    urls.getResearcherUrl().add(other);

    bio.setContactDetails(new ContactDetails());
    bio.getContactDetails()
        .setEmail(Arrays.asList(new Email("*****@*****.**"), new Email("*****@*****.**")));
    bio.getContactDetails().setAddress(new Address());
    bio.getContactDetails().getAddress().setCountry(new Country(Iso3166Country.GB));
    orcidMessage.setOrcidProfile(orcidProfile1);
    return orcidMessage;
  }
  private Response getOrcidMessageResponse(OrcidMessage orcidMessage, String requestedOrcid) {
    boolean isProfileDeprecated = false;
    if (orcidMessage == null) {
      Map<String, String> params = new HashMap<String, String>();
      params.put("orcid", requestedOrcid);
      throw new OrcidNotFoundException(params);
    }
    OrcidProfile orcidProfile = orcidMessage.getOrcidProfile();
    if (orcidProfile != null) {
      orcidProfile.setOrcidInternal(null);
      // If profile is deprecated
      if (orcidMessage.getOrcidProfile().getOrcidDeprecated() != null) {
        isProfileDeprecated = true;
      }
    }

    Response response = null;

    if (isProfileDeprecated) {
      Map<String, String> params = new HashMap<String, String>();
      params.put(
          "orcid",
          orcidProfile.getOrcidDeprecated().getPrimaryRecord().getOrcidIdentifier().getUri());
      throw new OrcidDeprecatedException(params);
    } else {
      response = Response.ok(orcidMessage).build();
    }

    return response;
  }
  @Test
  public void missingCreditName() throws Exception {

    ByteArrayOutputStream entityStream = new ByteArrayOutputStream(1024);
    OrcidMessage fakeBio = fakeBio();
    // empty creditName
    fakeBio.getOrcidProfile().getOrcidBio().getPersonalDetails().setCreditName(null);
    rdfWriter.writeTo(
        fakeBio,
        OrcidMessage.class,
        null,
        null,
        new MediaType("text", "turtle"),
        null,
        entityStream);

    String str = entityStream.toString("utf-8");
    System.out.println(str);
    // Should NOT include a foaf:name
    assertFalse(str.contains("foaf:name"));
    // but do include a concatenation as a label
    assertTrue(str.contains("rdfs:label"));
    assertTrue(str.contains("\"John Doe\""));
    // And family/given
    assertTrue(str.contains("foaf:familyName"));
    assertTrue(str.contains("\"Doe\""));
    assertTrue(str.contains("foaf:givenName"));
    assertTrue(str.contains("\"John\""));
  }
 /** See {@link OrcidApiServiceDelegator}{@link #searchByQuery(Map)} */
 @Override
 @VisibilityControl
 public Response searchByQuery(Map<String, List<String>> queryMap) {
   OrcidMessage orcidMessage = orcidSearchManager.findOrcidsByQuery(queryMap);
   List<OrcidSearchResult> searchResults =
       orcidMessage.getOrcidSearchResults() != null
           ? orcidMessage.getOrcidSearchResults().getOrcidSearchResult()
           : null;
   List<OrcidSearchResult> filteredResults = new ArrayList<OrcidSearchResult>();
   OrcidSearchResults orcidSearchResults = new OrcidSearchResults();
   if (searchResults != null) {
     orcidSearchResults.setNumFound(orcidMessage.getOrcidSearchResults().getNumFound());
     if (searchResults.size() > 0) {
       for (OrcidSearchResult searchResult : searchResults) {
         OrcidSearchResult filteredSearchResult = new OrcidSearchResult();
         OrcidProfile filteredProfile = new OrcidProfile();
         filteredSearchResult.setRelevancyScore(searchResult.getRelevancyScore());
         filteredProfile.setOrcid(searchResult.getOrcidProfile().getOrcid());
         filteredProfile.setOrcidId(searchResult.getOrcidProfile().getOrcidId());
         filteredProfile.setOrcidIdentifier(searchResult.getOrcidProfile().getOrcidIdentifier());
         filteredProfile.setOrcidBio(searchResult.getOrcidProfile().getOrcidBio());
         filteredSearchResult.setOrcidProfile(filteredProfile);
         filteredResults.add(filteredSearchResult);
       }
     }
   }
   orcidSearchResults.getOrcidSearchResult().addAll(filteredResults);
   return getOrcidSearchResultsResponse(orcidSearchResults, queryMap.toString());
 }
  private Response orcidWithMultipleResults() {

    OrcidMessage orcidMessage = new OrcidMessage();
    OrcidProfile orcidProfile1 = new OrcidProfile();
    OrcidProfile orcidProfile2 = new OrcidProfile();
    OrcidProfile orcidProfile3 = new OrcidProfile();

    OrcidSearchResult orcidSearchResult1 = new OrcidSearchResult();
    OrcidSearchResult orcidSearchResult2 = new OrcidSearchResult();
    OrcidSearchResult orcidSearchResult3 = new OrcidSearchResult();

    orcidSearchResult1.setOrcidProfile(orcidProfile1);
    orcidSearchResult2.setOrcidProfile(orcidProfile2);
    orcidSearchResult3.setOrcidProfile(orcidProfile3);

    List<OrcidSearchResult> searchResults = new ArrayList<OrcidSearchResult>();
    searchResults.add(orcidSearchResult1);
    searchResults.add(orcidSearchResult2);
    searchResults.add(orcidSearchResult3);

    OrcidSearchResults orcidSearchResults = new OrcidSearchResults();
    orcidSearchResults.getOrcidSearchResult().addAll(searchResults);
    orcidMessage.setOrcidSearchResults(orcidSearchResults);
    return Response.ok(orcidMessage).build();
  }
  @Test
  public void useAuthorizationCodeWithInalidScopesTest()
      throws InterruptedException, JSONException {
    String currentUrl =
        OauthAuthorizationPageHelper.loginAndAuthorize(
            this.getWebBaseUrl(),
            this.getClient1ClientId(),
            this.getClient1RedirectUri(),
            ScopePathType.ORCID_WORKS_CREATE.value(),
            null,
            this.getUser1UserName(),
            this.getUser1Password(),
            true,
            webDriver);
    Matcher matcher = AUTHORIZATION_CODE_PATTERN.matcher(currentUrl);
    assertTrue(matcher.find());
    String authorizationCode = matcher.group(1);
    assertFalse(PojoUtil.isEmpty(authorizationCode));

    ClientResponse tokenResponse =
        getClientResponse(
            this.getClient1ClientId(),
            this.getClient1ClientSecret(),
            ScopePathType.ORCID_WORKS_UPDATE.getContent(),
            this.getClient1RedirectUri(),
            authorizationCode);

    assertEquals(401, tokenResponse.getStatus());
    OrcidMessage result = tokenResponse.getEntity(OrcidMessage.class);
    assertNotNull(result);
    assertNotNull(result.getErrorDesc());
    assertEquals(
        "OAuth2 problem : Invalid scopes: /orcid-works/update available scopes for this code are: [/orcid-works/create]",
        result.getErrorDesc().getContent());
  }
 private void registerSearchMetrics(Response results) {
   OrcidMessage orcidMessage = (OrcidMessage) results.getEntity();
   if (orcidMessage != null
       && orcidMessage.getOrcidSearchResults() != null
       && !orcidMessage.getOrcidSearchResults().getOrcidSearchResult().isEmpty()) {
     return;
   }
 }
 private OrcidMessage getLegacy500OrcidEntity(Throwable e) {
   OrcidMessage entity = new OrcidMessage();
   entity.setMessageVersion(OrcidMessage.DEFAULT_VERSION);
   entity.setErrorDesc(
       new ErrorDesc(
           StringUtils.isNotBlank(e.getMessage())
               ? e.getMessage()
               : messageSource.getMessage(
                   "apiError.unknown.exception", null, localeManager.getLocale())));
   return entity;
 }
  private Response getOrcidSearchResultsResponse(
      OrcidSearchResults orcidSearchResults, String query) {

    if (orcidSearchResults != null) {
      OrcidMessage orcidMessage = new OrcidMessage();
      orcidMessage.setMessageVersion("1.2");
      orcidMessage.setOrcidSearchResults(orcidSearchResults);
      return Response.ok(orcidMessage).build();
    } else {
      Object params[] = {query};
      throw new NoResultException(
          localeManager.resolveMessage("apiError.no_search_result.exception", params));
    }
  }
 @Test
 public void testUpgradeMessage() {
   Reader reader =
       new InputStreamReader(
           getClass()
               .getResourceAsStream(
                   "/org/orcid/core/version/orcid-public-full-message-v1.0.23.xml"));
   OrcidMessage oldMessage = OrcidMessage.unmarshall(reader);
   OrcidMessageVersionConverter converter = new OrcidMessageVersionConverterImplV1_0_23ToV1_1();
   OrcidMessage newMessage = converter.upgradeMessage(oldMessage);
   assertNotNull(newMessage);
   assertEquals("1.1", newMessage.getMessageVersion());
   assertEquals(
       "4444-4444-4444-4446", newMessage.getOrcidProfile().getOrcidIdentifier().getPath());
   assertEquals(
       "http://orcid.org/4444-4444-4444-4446",
       newMessage.getOrcidProfile().retrieveOrcidUriAsString());
 }
 @Override
 @VisibilityControl(removeAttributes = false)
 @AccessControl(requiredScope = ScopePathType.READ_PUBLIC, enableAnonymousAccess = true)
 @NonLocked
 public Response findWorksDetailsFromPublicCache(String orcid) {
   try {
     OrcidMessage orcidMessage = orcidSearchManager.findPublicProfileById(orcid);
     if (orcidMessage != null) {
       OrcidProfile orcidProfile = orcidMessage.getOrcidProfile();
       if (orcidProfile != null) {
         orcidProfile.downgradeToWorksOnly();
       }
     }
     return getOrcidMessageResponse(orcidMessage, orcid);
   } catch (OrcidSearchException e) {
     LOGGER.warn("Error searching, so falling back to DB", e);
     return findWorksDetails(orcid);
   }
 }
 private OrcidMessage getLegacyOrcidEntity(String prefix, Throwable e) {
   OrcidMessage entity = new OrcidMessage();
   entity.setMessageVersion(OrcidMessage.DEFAULT_VERSION);
   entity.setErrorDesc(new ErrorDesc(prefix + e.getMessage()));
   return entity;
 }