Search in sources :

Example 6 with OsmDto

use of io.jawg.osmcontributor.rest.dtos.osm.OsmDto in project osm-contributor by jawg.

the class StoreLoginManager method isValidLogin.

/**
 * Calls the permissions web service to determine if the user provided has editing capabilities
 *
 * @return editing capabilities or not (false also if an retrofit error occurred)
 */
@Override
public boolean isValidLogin(final String login, final String password) {
    try {
        Call<OsmDto> callPerms;
        OsmDto permissions;
        Map<String, String> oAuthParams = loginPreferences.retrieveOAuthParams();
        // OAuth connection
        if (oAuthParams != null) {
            String requestUrl = BuildConfig.BASE_OSM_URL + "permissions";
            OAuthRequest oAuthRequest = new OAuthRequest(oAuthParams.get(CONSUMER_PARAM), oAuthParams.get(CONSUMER_SECRET_PARAM));
            oAuthRequest.initParam(OAuthParams.getOAuthParams().put(TOKEN_PARAM, oAuthParams.get(TOKEN_PARAM)).toMap());
            oAuthRequest.setOAuthToken(oAuthParams.get(TOKEN_PARAM));
            oAuthRequest.setOAuthTokenSecret(oAuthParams.get(TOKEN_SECRET_PARAM));
            oAuthRequest.setRequestUrl(requestUrl);
            oAuthRequest.signRequest(Verb.GET);
            oAuthRequest.encodeParams();
            String headerRequest = oAuthRequest.getOAuthHeader();
            callPerms = osmRestClient.getPermissions(headerRequest);
        } else {
            // Basic Auth connection
            String authorization = "Basic " + Base64.encodeToString((login.trim() + ":" + password).getBytes(), Base64.NO_WRAP);
            callPerms = osmRestClient.getPermissions(authorization);
        }
        Response<OsmDto> response = callPerms.execute();
        if (response.isSuccessful()) {
            permissions = response.body();
            if (permissions != null) {
                if (permissions.getPermissionsDto() != null && permissions.getPermissionsDto().getPermissionDtoList() != null) {
                    for (PermissionDto permissionDto : permissions.getPermissionsDto().getPermissionDtoList()) {
                        if ("allow_write_api".equals(permissionDto.getName())) {
                            return true;
                        }
                    }
                }
            }
        }
    } catch (IOException e) {
        Timber.e("Couldn't request permissions " + e);
    }
    return false;
}
Also used : OsmDto(io.jawg.osmcontributor.rest.dtos.osm.OsmDto) OAuthRequest(io.jawg.osmcontributor.rest.security.OAuthRequest) PermissionDto(io.jawg.osmcontributor.rest.dtos.osm.PermissionDto) IOException(java.io.IOException)

Example 7 with OsmDto

use of io.jawg.osmcontributor.rest.dtos.osm.OsmDto in project osm-contributor by jawg.

the class OSMSyncWayManager method getPoiWaysToUpdate.

/**
 * Download from backend all the NodeRefs to update as POIs.
 *
 * @return The list of POIs to update.
 */
private List<Poi> getPoiWaysToUpdate() {
    final List<Long> ids = poiNodeRefDao.queryAllUpdated();
    List<Poi> pois = new ArrayList<>();
    if (ids != null && !ids.isEmpty()) {
        Call<OsmDto> callOsm = osmRestClient.getNode(formatIdList(ids));
        try {
            Response<OsmDto> response = callOsm.execute();
            if (response.isSuccessful()) {
                OsmDto osmDto = response.body();
                if (osmDto != null) {
                    List<NodeDto> nodeDtoList = osmDto.getNodeDtoList();
                    pois = poiMapper.convertDtosToPois(nodeDtoList, false);
                }
                return pois;
            }
        } catch (IOException e) {
            Timber.e(e, e.getMessage());
        }
        Timber.w("The poi with id %s couldn't be found on OSM", 1);
    }
    return pois;
}
Also used : OsmDto(io.jawg.osmcontributor.rest.dtos.osm.OsmDto) ArrayList(java.util.ArrayList) Poi(io.jawg.osmcontributor.model.entities.Poi) IOException(java.io.IOException) NodeDto(io.jawg.osmcontributor.rest.dtos.osm.NodeDto)

Example 8 with OsmDto

use of io.jawg.osmcontributor.rest.dtos.osm.OsmDto in project osm-contributor by jawg.

the class OsmBackend method initializeTransaction.

/**
 * {@inheritDoc}
 */
@Override
public String initializeTransaction(String comment) {
    final OsmDto osmDto = new OsmDto();
    ChangeSetDto changeSetDto = new ChangeSetDto();
    List<TagDto> tagDtos = new ArrayList<>();
    osmDto.setChangeSetDto(changeSetDto);
    tagDtos.add(new TagDto(comment, "comment"));
    tagDtos.add(new TagDto(BuildConfig.APP_NAME + " " + BuildConfig.VERSION_NAME, "created_by"));
    changeSetDto.setTagDtoList(tagDtos);
    Call<ResponseBody> callChangeSet;
    if (loginPreferences.retrieveOAuthParams() != null) {
        callChangeSet = osmRestClient.addChangeSet(AuthenticationRequestInterceptor.getOAuthRequest(loginPreferences, BuildConfig.BASE_OSM_URL + "changeset/create", Verb.PUT).getOAuthHeader(), osmDto);
    } else {
        callChangeSet = osmRestClient.addChangeSet(osmDto);
    }
    try {
        Response<ResponseBody> response = callChangeSet.execute();
        if (response.isSuccessful() && response.body() != null) {
            return response.body().string();
        }
    } catch (IOException e) {
        Timber.e("Retrofit error, couldn't create Changeset!");
    }
    bus.post(new SyncUploadRetrofitErrorEvent(-1L));
    return null;
}
Also used : OsmDto(io.jawg.osmcontributor.rest.dtos.osm.OsmDto) ChangeSetDto(io.jawg.osmcontributor.rest.dtos.osm.ChangeSetDto) TagDto(io.jawg.osmcontributor.rest.dtos.osm.TagDto) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SyncUploadRetrofitErrorEvent(io.jawg.osmcontributor.rest.events.error.SyncUploadRetrofitErrorEvent) ResponseBody(okhttp3.ResponseBody)

Example 9 with OsmDto

use of io.jawg.osmcontributor.rest.dtos.osm.OsmDto in project osm-contributor by jawg.

the class OSMSyncNoteManager method syncDownloadNotesInBox.

/**
 * {@inheritDoc}
 */
@Override
public List<Note> syncDownloadNotesInBox(final Box box) {
    Timber.d("Requesting osm for notes download");
    String strBox = box.getWest() + "," + box.getSouth() + "," + box.getEast() + "," + box.getNorth();
    Call<OsmDto> osmDtoCall = osmRestClient.getNotes(strBox);
    try {
        Response<OsmDto> response = osmDtoCall.execute();
        if (response.isSuccessful()) {
            OsmDto osmDto = response.body();
            if (osmDto != null) {
                List<NoteDto> noteDtos = osmDto.getNoteDtoList();
                if (noteDtos != null && !noteDtos.isEmpty()) {
                    Timber.d("Updating %d note(s)", noteDtos.size());
                    bus.post(new SyncDownloadRetrofitErrorEvent());
                    return noteMapper.convertNoteDtosToNotes(noteDtos);
                }
            }
        }
    } catch (IOException e) {
        Timber.e(e, e.getMessage());
    }
    return null;
}
Also used : OsmDto(io.jawg.osmcontributor.rest.dtos.osm.OsmDto) SyncDownloadRetrofitErrorEvent(io.jawg.osmcontributor.rest.events.error.SyncDownloadRetrofitErrorEvent) IOException(java.io.IOException) NoteDto(io.jawg.osmcontributor.rest.dtos.osm.NoteDto)

Example 10 with OsmDto

use of io.jawg.osmcontributor.rest.dtos.osm.OsmDto in project osm-contributor by jawg.

the class OSMSyncNoteManager method remoteAddComment.

/**
 * {@inheritDoc}
 */
public Note remoteAddComment(final Comment comment) {
    Call<OsmDto> osmDtoCall;
    switch(comment.getAction()) {
        case Comment.ACTION_CLOSE:
            osmDtoCall = osmRestClient.closeNote(comment.getNote().getBackendId(), comment.getText(), "");
            break;
        case Comment.ACTION_REOPEN:
            osmDtoCall = osmRestClient.reopenNote(comment.getNote().getBackendId(), comment.getText(), "");
            break;
        case Comment.ACTION_OPEN:
            osmDtoCall = osmRestClient.addNote(comment.getNote().getLatitude(), comment.getNote().getLongitude(), comment.getText(), "");
            break;
        default:
            osmDtoCall = osmRestClient.addComment(comment.getNote().getBackendId(), comment.getText(), "");
            break;
    }
    try {
        Response<OsmDto> response = osmDtoCall.execute();
        if (response.isSuccessful()) {
            OsmDto osmDto = response.body();
            if (osmDto != null) {
                NoteDto noteDto = osmDto.getNoteDtoList().get(0);
                Note note = noteMapper.convertNoteDtoToNote(noteDto);
                note.setUpdated(false);
                note.setId(comment.getNote().getId());
                return note;
            }
        } else if (response.code() == 409) {
            bus.post(new SyncConflictingNoteErrorEvent(comment.getNote()));
        }
    } catch (IOException e) {
        Timber.e(e, "Retrofit error, couldn't create comment !");
    }
    bus.post(new SyncUploadNoteRetrofitErrorEvent(comment.getNote().getId()));
    return null;
}
Also used : OsmDto(io.jawg.osmcontributor.rest.dtos.osm.OsmDto) SyncConflictingNoteErrorEvent(io.jawg.osmcontributor.rest.events.error.SyncConflictingNoteErrorEvent) Note(io.jawg.osmcontributor.model.entities.Note) SyncUploadNoteRetrofitErrorEvent(io.jawg.osmcontributor.rest.events.error.SyncUploadNoteRetrofitErrorEvent) IOException(java.io.IOException) NoteDto(io.jawg.osmcontributor.rest.dtos.osm.NoteDto)

Aggregations

OsmDto (io.jawg.osmcontributor.rest.dtos.osm.OsmDto)11 IOException (java.io.IOException)9 NodeDto (io.jawg.osmcontributor.rest.dtos.osm.NodeDto)5 WayDto (io.jawg.osmcontributor.rest.dtos.osm.WayDto)4 ResponseBody (okhttp3.ResponseBody)4 ArrayList (java.util.ArrayList)3 Poi (io.jawg.osmcontributor.model.entities.Poi)2 NoteDto (io.jawg.osmcontributor.rest.dtos.osm.NoteDto)2 NonNull (android.support.annotation.NonNull)1 Note (io.jawg.osmcontributor.model.entities.Note)1 PoiType (io.jawg.osmcontributor.model.entities.PoiType)1 ChangeSetDto (io.jawg.osmcontributor.rest.dtos.osm.ChangeSetDto)1 PermissionDto (io.jawg.osmcontributor.rest.dtos.osm.PermissionDto)1 TagDto (io.jawg.osmcontributor.rest.dtos.osm.TagDto)1 SyncConflictingNoteErrorEvent (io.jawg.osmcontributor.rest.events.error.SyncConflictingNoteErrorEvent)1 SyncDownloadRetrofitErrorEvent (io.jawg.osmcontributor.rest.events.error.SyncDownloadRetrofitErrorEvent)1 SyncUploadNoteRetrofitErrorEvent (io.jawg.osmcontributor.rest.events.error.SyncUploadNoteRetrofitErrorEvent)1 SyncUploadRetrofitErrorEvent (io.jawg.osmcontributor.rest.events.error.SyncUploadRetrofitErrorEvent)1 OAuthRequest (io.jawg.osmcontributor.rest.security.OAuthRequest)1 Map (java.util.Map)1